Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ffmpeg join two mp4 videos one after another

I've got 2 videos I want them to join into one video using ffmpeg. I am running these commands in Java so I need it to use the command line so no other programme solutions apply. I have tried many different ways but non seem to work. So i got a video of 35 seconds and one of 5 seconds I need a output of 40 seconds this never seems to happen. if anyone could help it would be greatly appreciated

like image 798
Grbe1l Avatar asked Aug 21 '15 10:08

Grbe1l


People also ask

How do I combine two videos in FFmpeg?

Use FFmpeg concat to merge videos For videos that have the same codec, you can concatenate them using concat demuxer or concat protocol.

Does mp4 support file level concatenation?

Some container formats such as mp4 cannot be directly concatenated using the Linux kernel's cat command or FFmpeg's concat command. An option for this is to convert your files to an intermediate file format and then concatenate them easily. A common intermediate file format is ts (Transport Stream).


1 Answers

I have found a solution to this myself and I believe this could be useful for other people as it took me a lot of time to find it. You can not join two mp4 together through ffmpeg. You can however convert the videos to a .ts file then join them one after another and convert it back to a mp4. This sounds complicated but the ffmpeg is pretty simple.

ffmpeg -i vid1.mp4 -c copy -bsf h264_mp4toannexb  vid1.ts
ffmpeg -i vid2.mp4 -c copy -bsf h264_mp4toannexb  vid2.ts
ffmpeg -i "concat:vid1.ts|vid2.ts" -c copy output.mp4       

So the first 2 lines convert the videos to a .ts file and the 3rd line joins them and makes a .mp4 and obvously you can delete the .ts files if you are running this in a programming language like java.

Hope someone finds this useful as I spent a greqat deal of time trying to find this solution.

like image 153
Grbe1l Avatar answered Sep 28 '22 07:09

Grbe1l