Friday, 5 May 2017

Gambas MP4Box: a batch .h264 to .mp4 video packager

Using RaspiVid software with a RaspiCam results in a .h264 encoded video file.

Most media players require .h264 to be "packaged" into a suitable video container format like .mp4.

This very simple Gambas project uses the program MP4Box to convert or package a bunch of .h264 files into .mp4 files ready to be edited or played using popular applications.


This time of the year we enjoy watching garden birds inside their nests via our 2 Raspberry Pi powered bird boxes. These systems stream video, and also "motion capture" low frame rate video clips. But when I need a video clip with higher quality, I use the RaspiVid software.

The system is set up to produce a 30s video file using RaspiVid every time I click on a button. These .h264 files then have to be transfered via wifi, packaged as MP4 and then viewed and/or edited.

H264 is a compression standard which results in reasonably good quality video, but compressed to occupy less "space". MP4 is a media container format, and typically includes separate streams for video and audio. In this case my resulting MP4 just contains the compressed video data from an H264 file.

MP4Box is a simple to use command-line media packager. To create a new MP4 file from an existing H264 file, you can use it like this:-

MP4Box -add myVideo.h264 myVideo.mp4

To save having to type this each time, I've finally got around to writing a Gambas program to do it the gui way.

The Gambas program

I typically have a bunch of .h264 files that I have downloaded from my two bird box systems, and saved to a common folder on my laptop. I want my program to select the folder and then list only the .h264 files in this folder.

Then I want to package/convert them all with one press of the Convert button.

And then finally make the choice of playing or editing a single .mp4 file, by left-clicking the required file, which loads either gnome-mplayer or Avidemux.

The code

The main form needs the following controls positioned carefully upon it:-
  • Button
  • DirChooser
  • ListBox  x2
  • Frame
  • RadioButton   x2

Declare two constants:-

Const VIDEO_PLAYER As String = "gnome-mplayer"      'our chosen video player
Const VIDEO_EDITOR As String = "avidemux3_qt5"      'our chosen video editor

Set initial folder for DirChooser:-

Public Sub Form_Open()

  DirCh.SelectedPath = User.Home & "/Videos"


Populate ListBox with any .h264 files when a directory is selected:-

Public Sub DirCh_Change()
Dim strFileName As String

  btnConvert.Enabled = False
  For Each strFileName In Dir(DirCh.SelectedPath, "*.h264").Sort()
    btnConvert.Enabled = True

Batch convert all .mp4 files when button is pressed:-

Public Sub btnConvert_Click()
Dim strFileName As String
Dim strNameNoExt As String

  For Each strFileName In Dir(DirCh.SelectedPath, "*.h264").Sort()
    strNameNoExt = Mid(strFileName, 1, InStr(strFileName, ".") - 1)
    Exec ["MP4Box", "-add", DirCh.SelectedPath & "/" & strFileName, DirCh.SelectedPath & "/" & strNameNoExt & ".mp4"] Wait
  For Each strFileName In Dir(DirCh.SelectedPath, "*.mp4").Sort()

Open and Play or edit selected file when mouse is left-clicked over a file:-

Public Sub lstMP4_Click()
Dim strAction As String

  If radPlay.Value = True Then
    strAction = VIDEO_PLAYER
    strAction = VIDEO_EDITOR
  If lstMP4.Text <> "" Then
    Exec [strAction, DirCh.SelectedPath & "/" & lstMP4.Text]


That's it, couldn't be much simpler!

Just change the Constants if you want to launch other players or editors, and don't forget to tart-it-up a bit by adding Tooltip text.

No comments:

Post a Comment