How can I merge two mp4 files without losing quality?

51

32

I have two high-quality mp4 files, which are of two parts of the same show.

I want to merge them together into 1 file, and I usually use VirtualDub to merge my files... But it doesn't seem to support mp4.

What alternatives are available (especially ones that don't require re-encoding)?

Paolo Bergantino

Posted 2009-09-18T22:20:24.967

Reputation: 2 809

Answers

41

I usually use VirtualDub to merge my files, but it doesn't seem to support mp4. So what alternatives are there?

Avidemux. It's just like VirtualDub, only with more formats supported and built-in codecs and filters. I always use it in preference; it joins AVC+AAC.MP4 files (without recoding) fine for me.

To join them in AviDemux:

  1. File -> Open -> Select File
  2. File -> Append -> Select File
  3. Set all to Copy
  4. File -> Save -> FileName
  5. Wait..

According to AViDemux forums, this join process is lossless (http://www.avidemux.org/smf/index.php?topic=9467.0)

bobince

Posted 2009-09-18T22:20:24.967

Reputation: 8 816

This didn't work for me. I concatenated 3 sequences of videos (3 or 4 individual videos each) and once it worked, once only the first video worked, and once only the first 2 videos worked. The remainder of the concatenated videos was just a gray image, even though the resulting videos were pretty much exactly as big as the ones they were supposed to be the concatenated versions of. This other answer, however, worked just fine.

– UTF-8 – 2017-09-27T22:13:19.257

yeah, it seems to do excellent with keeping audio in sync with video whereas the other persons answers to this question are questionable on that IMHO. – djangofan – 2010-11-12T23:39:09.977

1I tried using Avidemux to join two files together but the audio became out of sync. – GiddyUpHorsey – 2012-07-02T13:56:23.653

You can get sync problems when the amount of audio material available doesn't match the length of video frames, or there's any stream corruption. You may be able to fix this up with the 'stream fix' features of VideoRedo or ProjectX. – bobince – 2012-07-02T21:38:45.367

I just tried this (H264 video) and it lost frames at the joins unless they were I-frames, as somewhat described here. Useless.

– Christopher Galpin – 2013-06-11T01:09:56.033

1

But this method worked: http://superuser.com/a/255566/1780 Hooray!

– Christopher Galpin – 2013-06-11T01:15:33.750

4If you use AviDemux to join two Mp4 files, don't forget to select the Mp4 Demux option at the bottom left hand corner. Otherwise it may try to convert it to an avi! – None – 2013-08-11T05:12:58.167

29

You can do this with ffmpeg:

mkfifo temp0 temp1
ffmpeg -i input0.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts -y temp0 2> /dev/null & \
ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts -y temp1 2> /dev/null & \
ffmpeg -f mpegts -i "concat:temp0|temp1" -c copy -absf aac_adtstoasc output.mp4

This doesn't re-encode anything, it places them in a new transport stream container, which makes them more easy to concatenate, and then concatenates them back into an MP4. If output.mp4 already exists, the command will fail. The version above uses named pipes, if you're on a system that doesn't support those you'd have to use intermediate files (like windows):

ffmpeg -i input0.mp4 -c copy -bsf:v h264_mp4toannexb temp0.ts
ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb temp1.ts
ffmpeg -i "concat:temp0.ts|temp1.ts" -c copy -bsf:a aac_adtstoasc output.mp4

evilsoup

Posted 2009-09-18T22:20:24.967

Reputation: 10 085

can someone explain how this is done with avconv instead of ffmpeg? I was able to complete the first two steps, but the last doesn't recognize the option -absf – becko – 2014-10-01T23:45:09.023

@becko See my edit. Now you should use a stream specifier (see here https://libav.org/avconv.html#Advanced-options).

– slhck – 2014-10-02T12:46:46.940

2+1 just for future reference, you can substitute avconv for ffmpeg in the above commands and it will work. – becko – 2014-10-02T14:00:44.600

15

Concat demuxer

The concat demuxer was added to ffmpeg 1.1. If your version of ffmpeg is to old, get the newest static binary from here: http://www.ffmpeg.org/download.html

Instructions

Create a file mylist.txt with all the files you want to have concatenated in the following form (Lines starting with a dash are ignored):

# this is a comment
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'

Note that these can be either relative or absolute paths. Then you can encode your files with:

ffmpeg -f concat -i mylist.txt -c copy output

It is possible to generate this list file with a bash for loop, or using printf. Either one of the following would generate a list file containing every *.wav in the working directory:

for f in ./*.wav; do echo "file '$f'" >> mylist.txt; done
printf "file '%s'\n" ./*.wav > mylist.txt

Source: ffmpeg wiki

erik

Posted 2009-09-18T22:20:24.967

Reputation: 1 519

8

YAMB along with MP4BOX is a good option.
Download both and unzip them (no installation needed and together they're about 5MB), in YAMB choose Settings and set the folder path of MP4box.
To join mp4 files choose Editing > Click to Join supported...

Eran

Posted 2009-09-18T22:20:24.967

Reputation: 3 201

1i had trouble using this method because of audio sync problems. it was a nightmare... not trying it again. – djangofan – 2010-11-12T23:37:51.833

1Seems like the best solution, keeping the .mp4 container format which would seem the more attractive option to the author ^^ – Oskar Duveborn – 2009-09-19T13:44:17.913

1After difficulty, I used this config: MP4Box_path=E:\Software\Video Editing\Yamb-2.1.0.0_beta2\MP4Box\MP4Box.exe in an attempt to merge 4 500MB MP4 files generated from AVIDemux . It didn't work. The program just sat there for 45 minutes doing nothing until I finally gave up. – djangofan – 2009-11-27T23:43:26.880

6

SUPER © (Simplified Universal Player Encoder & Renderer) provides the following additional Output Process:

Any supported input Multimedia file can be processed:

      o Join Format-Identical Files.
      o Mux Video & Audio Streams.
      o DeMux Extract Streams. 

of course, MP4 is a supported format.

SUPER © is freeware.

Molly7244

Posted 2009-09-18T22:20:24.967

Reputation:

5Its user interface is the most horrible I've seen in my life. I'm not kidding, it's just horrible. I emphasize it once more: I can't name a worse interface, I'm serious about it, I've never seen anything below the "SUPER threshold". – Camilo Martin – 2012-01-12T14:48:03.233

5

Try using the Matroska video container.

Firstly, you need mkvtoolnix. Download and install it, then you need to fire up mkvmerge GUI (unless you like command-line tools, which is fine by me - although you might want to look at the file linking section of the mkvmerge documentation).

Add your first file by clicking "add". Then, click on "append", and open your second file. Set the output file, hit "Start muxing" at the bottom, and away you go!

If the container is unsuitable for your needs, you can reencode it, or try to convert it to some other format... Although MKV is a very lovely container!

Breakthrough

Posted 2009-09-18T22:20:24.967

Reputation: 32 927

it has to be muxed? i cant be joined without muxing? – djangofan – 2009-11-27T23:45:07.487

Well, if you're using an MKV container, technically you are remuxing it.

Try it, there is no real time penalty... – Breakthrough – 2009-12-07T02:09:11.960

1

If you can play them on your system / have a codec installed, Windows Movie Maker may the quickest (and free) way to join the two files. You can then use Virtualdub to convert to a different format.

William Hilsum

Posted 2009-09-18T22:20:24.967

Reputation: 111 572

Audacity seems to be for audio files. This is a video file. – Paolo Bergantino – 2009-09-18T22:40:46.267

So sorry, I mis-read, just saw the MP part and my brain though audacity. – William Hilsum – 2009-09-18T23:01:53.697

Editing answer... – William Hilsum – 2009-09-18T23:02:24.100

AFAIK, Movie Maker doesn't support mp4 and converting to a different format means lose of quality which the question specifies as unwanted. – Eran – 2009-09-18T23:16:54.243

I tried MovieMaker on windows7 and the audio becomes out of sync when joining MP4 files into a WMV. This option wont work. – djangofan – 2009-11-27T23:10:28.070

1

Another solution is Open Video Joiner. It also does transition effects.

enter image description here

harrymc

Posted 2009-09-18T22:20:24.967

Reputation: 306 093

It's a shareware though. and for 30$ it's not cheap just to merge video files – Eran – 2009-09-19T12:10:23.197

It was said to be freeware. Sorry if I was misled. – harrymc – 2009-09-19T15:51:19.300