Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate two MP4 files using FFmpeg?

Tags:

ffmpeg

h.264

mp4

I'm trying to concatenate two mp4 files using ffmpeg. I need this to be an automatic process hence why I chose ffmpeg. I'm converting the two files into .ts files and then concatenating them and then trying to encode that concatenated .ts file. The files are h264 and aac encoded and I'm hoping to keep the quality the same or as close to original as possible.

ffmpeg -i part1.mp4 -vcodec copy -vbsf h264_mp4toannexb -acodec copy part1.ts ffmpeg -i part2.mp4 -vcodec copy -vbsf h264_mp4toannexb -acodec copy part2.ts cat part1.ts part2.ts > parts.ts ffmpeg -y -i parts.ts -acodec copy -ar 44100 -ab 96k -coder ac -vbsf h264_mp4toannexb parts.mp4 

Unfortunately I'm getting the following error message coming back from ffmpeg during encoding:

[h264 @ 0x1012600]sps_id out of range [h264 @ 0x1012600]non-existing SPS 0 referenced in buffering period [h264 @ 0x1012600]sps_id out of range [h264 @ 0x1012600]non-existing SPS 0 referenced in buffering period [NULL @ 0x101d600]error, non monotone timestamps 13779431 >= 13779431kbits/s     av_interleaved_write_frame(): Error while opening file 

This happens about half way through encoding which makes me think that you can't concat two .ts files together and have it work.

like image 489
Mark L Avatar asked Sep 07 '11 11:09

Mark L


People also ask

How do I combine two MP4 files?

To merge MP4 files, select multiple MP4 files in the Media Library while holding down Ctrl or Shift key, and then drag and drop to the video track on the Timeline. All MP4 files will be played one after another without a gap after saving to your computer.

Does FFmpeg work with MP4?

FFmpeg can input most container formats natively, including MP4, . ts, MOV, AVI, Y4M, MKV, and many others. This is the output file.

How do I combine two videos in android programmatically FFmpeg?

run(new String[] { "ffmpeg", "-i", "'concat:" + ts1 + "|" + ts2 + "'", "-vcodec", "copy", "-acodec", "copy", "-absf", "aac_adtstoasc", output });


2 Answers

FFmpeg has three concatenation methods:

1. concat video filter

Use this method if your inputs do not have the same parameters (width, height, etc), or are not the same formats/codecs, or if you want to perform any filtering.

Note that this method performs a re-encode of all inputs. If you want to avoid the re-encode, you could re-encode just the inputs that don't match so they share the same codec and other parameters, then use the concat demuxer to avoid re-encoding everything.

ffmpeg -i opening.mkv -i episode.mkv -i ending.mkv \ -filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] \ concat=n=3:v=1:a=1 [v] [a]" \ -map "[v]" -map "[a]" output.mkv 

2. concat demuxer

Use this method when you want to avoid a re-encode and your format does not support file-level concatenation (most files used by general users do not support file-level concatenation).

$ cat mylist.txt file '/path/to/file1' file '/path/to/file2' file '/path/to/file3'      $ ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4 

For Windows:

(echo file 'first file.mp4' & echo file 'second file.mp4' )>list.txt ffmpeg -safe 0 -f concat -i list.txt -c copy output.mp4 

3. concat protocol

Use this method with formats that support file-level concatenation (MPEG-1, MPEG-2 PS, DV). Do not use with MP4.

ffmpeg -i "concat:input1|input2" -codec copy output.mkv 

This method does not work for many formats, including MP4, due to the nature of these formats and the simplistic concatenation performed by this method.


If in doubt about which method to use, try the concat demuxer.

Also see

  • FFmpeg FAQ: How can I join video files?
  • FFmpeg Wiki: How to concatenate (join, merge) media files
like image 108
rogerdpack Avatar answered Oct 06 '22 13:10

rogerdpack


FOR MP4 FILES

For .mp4 files (which I obtained from DailyMotion.com: a 50 minute tv episode, downloadable only in three parts, as three .mp4 video files) the following was an effective solution for Windows 7, and does NOT involve re-encoding the files.

I renamed the files (as file1.mp4, file2.mp4, file3.mp4) such that the parts were in the correct order for viewing the complete tv episode.

Then I created a simple batch file (concat.bat), with the following contents:

:: Create File List echo file file1.mp4 >  mylist.txt  echo file file2.mp4 >> mylist.txt echo file file3.mp4 >> mylist.txt  :: Concatenate Files ffmpeg -f concat -i mylist.txt -c copy output.mp4 

The batch file, and ffmpeg.exe, must both be put in the same folder as the .mp4 files to be joined. Then run the batch file. It will typically take less than ten seconds to run.
.

Addendum (2018/10/21) -

If what you were looking for is a method for specifying all the mp4 files in the current folder without a lot of retyping, try this in your Windows batch file instead (MUST include the option -safe 0):

:: Create File List for %%i in (*.mp4) do echo file '%%i'>> mylist.txt  :: Concatenate Files ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4 

This works on Windows 7, in a batch file. Don't try using it on the command line, because it only works in a batch file!

like image 22
Ed999 Avatar answered Oct 06 '22 14:10

Ed999