Combine H.264 MP4 Files and Add Audio via MP4Box and FFMPEG

Some late night fun… Since I can’t sleep… I’ve successfully found a means to combine multiple video files and add audio to a combined video progmatically and documenting my findings, thought I would share…

First I had to install GPAC which contains MP4Box, using it allows to combine the video files pre-existing as we have them now into one single file (1.mp4 and 2.mp4 are video files captured with an AXIS IP Camera in H.264):

MP4Box -cat 1.mp4 -cat 2.mp4 out.mp4

Next we determine the length in seconds of the video, this is where a custom PHP Script is involved that will determine the length:

$output = shell_exec("/usr/local/bin/ffmpeg -i {$path}");
preg_match('/Duration: ([0-9]{2}):([0-9]{2}):([^ ,])+/', $output, $matches);
$time = str_replace("Duration: ", "", $matches[0]);
$time_breakdown = explode(":", $time);
$total_seconds = round(($time_breakdown[0]*60*60) + ($time_breakdown[1]*60) + $time_breakdown[2]);

With the example file, here’s what the PHP script would see:

ffmpeg -i out.mp4 2>&1 | grep Duration
Duration: 00:00:13.60, start: 0.000000, bitrate: 875 kb/s

And then we cut the audio file accordingly, the above PHP script returned 14 seconds for our two combined videos, since 13.60 is rounded to 14 from MP4Box, so it starts at 00:00:00 and takes a total time of 14 seconds of the audio file and creates a new file to be used next.

ffmpeg -ss 00:00:00 -t 14 -i music.mp3 final.mp3

Take the combined videos from MP4Box and add the newly created 14 second final.mp3 file and create the final file for viewing.

ffmpeg -vcodec copy -i final.mp3 -i out.mp4 final.mp4

If your wanting to have progressive downloading for web playback:

MP4Box -inter 0.5 final.mp4

Core Action Automated Video System for Action Sports

I know I’ve not been very active with my postings as of lately however, there is good reason :)

I have been doing a lot of developing lately particularly for the release of Core Action‘s Automated Video system that is patent pending. Basically it is a system that was developed that allows kids from skate parks to have video of them skating and doing tricks and having them identified, recorded and uploaded to the web for later retrieval.

Currently the system has been successfully running at Eisenbergs skate park in Plano, Texas for over two weeks and utilized by over 200 kids riding at the park.  Over 7k Videos have been recorded so far, and over 100 kids have claimed their videos.

For those curious on some of the technical details. Every kid in the park that wants to have their videos recorded are handed a Core Action sticker that contains an Radio Frequency ID (RFID), placing the sticker on their helmet. There are hot zones setup within the park where an RFID reader and antennas are setup to detect when someone enters the area of an IP Camera mounted with a fish eye lens that is called upon to start recording and capture the persons tricks and or bail. Uniquely identifying this person with their tag for later retrieval online. After it has been captured the persons video is queued and played back on a big screen projector on the wall so they can see the videos they just created. While this is going on the video that is captured is then taken and uploaded processed so that it can be viewable/shared online at the Core Action website with anyone.

The middleware is compromised of many different systems. Including VLC and C# which makes up the bulk of the actual “cool process” of Reading RFID tags, Capture the IP Camera Streams in H.264 via RTSP and saving the data when the RFID tags are detected. VLC is also used to playback the videos the big screen at the park. PHP, FFMPEG, and MySQL are utilized on the server end to process the video files and create the proper entries based on the RFID tags recorded into a file system and MySQL database to be retrieved, via an online social network written as well in PHP and MySQL.

Myself and one other developer/programmer, Matt Stapleton are responsible for developing the system.

Overall the system took over a year of Research and Development to make it a reality, there are still a few minor things we are still fine tunning but obviously it is doing what we had set out to do. (see below :mrgreen: )