Microsoft Bob

Just a short, simple blog for Bob to share some tips and tricks.

Be sure to check out my non-technical blog at www.bobsbasement.net.

Month List

How to Merge a Folder of MP4 Files with FFmpeg

I ran into an interesting situation the other day: I had a bunch of H.264 MP4 video files which I had created with Handbrake that I needed to combine, and I didn't want to use my normal video editor (Sony Vegas) to perform the merge. I'm a big fan of FFmpeg, so I figured that there was some way to automate the merge without having to use an editor.

I did some searching around the Internet, and I couldn't find anyone who was doing exactly what I was doing, so I wrote my own batch file that combines some tricks that I have used to automate FFmpeg in the past with some ideas that I found through some video hacking forums. Here is the resulting batch file, which will combine all of the MP4 files in a directory into a single MP4 file named "foobar.mp4", which can be renamed to something else:

@echo off

REM Clean up any pre-existing temporary files
if exist foobar.mp4 del foobar.mp4
if exist foobar.tmp del foobar.tmp
if exist *.ts del *.ts

REM Remux MP4 files into individual transport streams
for /f "usebackq delims=|" %%a in (`dir /on /b *.mp4`) do (
ffmpeg.exe -i "%%a" -c copy -bsf h264_mp4toannexb -f mpegts "%%a.ts"
)

REM Create a file list of individual transport streams
for /f "usebackq delims=|" %%a in (`dir /b *.ts`) do (
echo file %%a>>foobar.tmp
)

REM Remux transport streams from the file list into merged MP4 file
ffmpeg.exe -f concat -i foobar.tmp -c copy -bsf aac_adtstoasc foobar.mp4

REM Clean up temporary files
if exist foobar.tmp del foobar.tmp
if exist *.ts del *.ts

The merging process in this batch file is performed in two steps:

  • First, all of the individual MP4 files are remuxed into individual transport streams
  • Second, all of the individual transport streams are remuxed into a merged MP4 file

Here are the URLs for the official documentation on each of the FFmpeg switches and parameters that I used:

By the way, I realize that there may be better ways to do this with FFmpeg, so I am open to suggestions. ;-]

Posted: Jul 06 2014, 00:04 by Bob | Comments (0)
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Social Bookmarks: E-mail | Kick it! | DZone it! | del.icio.us