38

I have several chunks in folder.

0001.mp4
0002.mp4
0003.mp4
...
0112.mp4

I would like to merge them into full.mp4

I tried to use:

avconv -f concat -i <(printf "file '%s'\n" /root/chunk/*.mp4) -y \
-c copy /root/test/full.mp4

Unknown input format: 'concat'

avconv -f concat -i <(printf "%s|" /root/chunk/*.mp4) -y \
-c copy /root/test/full.mp4

Unknown input format: 'concat'

avconv -i concat:`ls -ltr /root/chunk/*.mp4 | awk 'BEGIN {ORS="|"} { print $9 }'` \
 -c:v copy -c:a copy /root/test/full.mp4

In last edition only one input file was catched to output.

How to merge all chunks from folder into full video?

I don't want to use ffmpeg or other. Avconv only.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Alex
  • 1,397
  • 4
  • 15
  • 26

10 Answers10

34

mp4 files cannot be simply concatenated, as the "accepted" answer suggests.

If you run that, and that alone, you'll end up with output.mp4 having only the contents of file1.mp4.

That said, what you're looking to do in the original question can in fact be done, as long as you split original file into mpeg streams correctly.

The following commands will split input.mp4 into 3x 60 second segments, in file[1-3].ts:

avconv -ss 0 -i input.mp4 -t 60 -vcodec libx264 -acodec aac \
    -bsf:v h264_mp4toannexb -f mpegts -strict experimental -y file1.ts
avconv -ss 0 -i input.mp4 -t 60 -vcodec libx264 -acodec aac \
    -bsf:v h264_mp4toannexb -f mpegts -strict experimental -y file2.ts
avconv -ss 0 -i input.mp4 -t 60 -vcodec libx264 -acodec aac \
    -bsf:v h264_mp4toannexb -f mpegts -strict experimental -y file3.ts

You can then put them back together much as the other answer suggests:

avconv -i concat:"file1.ts|file2.ts|file3.ts" -c copy \
   -bsf:a aac_adtstoasc -y full.mp4

I used this process to create a scalable, parallel transcoder as described at:

Dustin Kirkland
  • 5,323
  • 3
  • 36
  • 34
  • 1
    This solution isn't working for me. If I use the word `concat` anywhere in the command line, I always get errors like `Unknown input format: 'concat'` or `Requested output format 'concat:test.mp3' is not a suitable output format`. Any suggestions? – Mr Lister Jul 30 '14 at 08:01
  • 1
    What version of avconv do you have? – Aaron Sep 09 '14 at 14:30
  • I'm having the same problem with avconv version 12. Version 11 doesn't work either – omnibrain Feb 14 '17 at 17:49
20
avconv -i concat:file1.mp4\|file2.mp4 -c copy output.mp4

I don't know if works with any container's type ( worked for me with AVI ).

Marcel Gwerder
  • 8,353
  • 5
  • 35
  • 60
test
  • 296
  • 2
  • 3
  • 10
    Didnt work for me... had to do it with mencoder... mencoder -oac pcm -ovc copy -o merged_file.mp4 `ls *.mp4` – Pipo Mar 02 '14 at 17:01
  • 6
    mp4 containers cannot be concatenated in this way. – Dustin Kirkland Jul 09 '14 at 14:47
  • mp4 and matroska cannot be concatenated. You need to recode into a concatenatable or randomly seekable container first, like MPEG-TS or maybe NUT. – Christian Hujer Jul 12 '15 at 17:21
  • 3
    The accepted answer did **not** work for me. Instead, I used the `MP4Box` command (from gpac package) as follows: `MP4Box video1.mp4 -cat video2.mp4 -cat video3.mp4 [...] -out video-full.mp4` – code_dredd Sep 23 '15 at 10:43
  • 1
    Nope, doesn't work. The net result is that the video is just as long as the first one, and the viewable content is exactly the content of the first video. Other videos seem to be ignored. – filmil May 27 '16 at 15:03
  • As @test stated it did, this technique worked for me with an avi wrapper. – Digger May 04 '17 at 02:45
10

For mp4 the only working solution I found was with MP4Box from gpac package

#!/bin/bash
filesList=""
for file in $(ls *.mp4|sort -n);do
    filesList="$filesList -cat $file"
done
MP4Box $filesList -new merged_files_$(date +%Y%m%d_%H%M%S).mp4

or command is

MP4Box -cat file1.mp4 -cat file2.mp4 -new mergedFile.mp4

with mencoder and avconv I could'nt make it work :-(

Pipo
  • 4,653
  • 38
  • 47
  • i'm on debian jessie and this is the only answer that i could run (mostly due to missing packages).the result is weird though, the actual stream is shorter than what VLC shows, and when seeking there it just restarts the playback. – Attila Lendvai Feb 27 '15 at 01:00
  • A shorter command line that will take in all mp4 files in the current directory is: `MP4Box $(printf -- "-cat %s " *.mp4) -new mergedFile.mp4` – filmil May 27 '16 at 15:12
6

this works

avconv -i 1.mp4 1.mpeg
avconv -i 2.mp4 2.mpeg
avconv -i 3.mp4 3.mpeg

cat 1.mpeg 2.mpeg 3.mpeg | avconv -f mpeg -i - -vcodec mpeg4 -strict experimental output.mp4

Above works if you only have avconv - however ffmpeg has added functions ... to quote :

"Above avconv steps does unnecessary additional lossy encoding steps which is slow and reduces the quality of the output. I would recommend using the concat demuxer (additional info) or concat filter instead, but avconv lacks those features available in ffmpeg from FFmpeg" – @LordNeckbeard

    SEE  https://trac.ffmpeg.org/wiki/Concatenate

If you're using a system that supports named pipes, you can use those to avoid creating intermediate files - this sends stderr (which ffmpeg sends all the written data to) to /dev/null, to avoid cluttering up the command-line:

mkfifo temp1 temp2
ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp1 2> /dev/null 
ffmpeg -i input2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp2 2> /dev/null 
ffmpeg -f mpegts -i "concat:temp1|temp2" -c copy -bsf:a aac_adtstoasc output.mp4
Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
  • Shows a lot of errors (at the last step), but the only answer that does result in a merged file for me so +1! – Mark Nov 14 '14 at 20:02
  • This almost works for me, but with a loss of quality... (the image is pixelized.) Any way to solve this problem? – S. Caruso Jul 29 '18 at 14:58
2

This one works for me:

avconv -i "concat:001.mp4|002.mp4|003.mp4" -c copy full.mp4

Just concats all the files into one mp4 without re-encoding them.

romaroma
  • 658
  • 8
  • 13
  • 1
    The only reason I found this page is because this solution just outputs `concat:: Protocol not found` (even though it's on the list of protocols output by `avconv -protocol`) – Jeremy List Apr 19 '17 at 06:04
  • Got the above error too in the inputs list with avconv 12.3 on Mac 10.13.6 with Homebrew. – Pysis Jun 24 '19 at 18:20
0

This method will do for concating raw *.avi files only, I suppose. But since lots of people willing to join avi files will end up viewing this question I'll post my answer here anyway:

cat *.avi > out_tmp.avi
avconv -i out_tmp.avi -c copy output.avi

One more time: works for uncompressed *.avi files only.

installero
  • 9,096
  • 3
  • 39
  • 43
0

I did that with avidemux (gtk tool). It is available as a package under most linuxes, also for Windows and Mac. I simply opened the first file. Then I added the next ones in order. Finally, I saved the concatenated video using audio and video copy and format mp4.

It worked great !

Pielo
  • 1
0

I am using ffmpeg command to do this. Here is example:

ffmpeg -i concat:/dev/null|file1.ts|file2.ts -c copy -bsf:a aac_adtstoasc -y output.mp4

Take a look at this github project for Distributed parallel video trascoding

KernelPanic
  • 2,328
  • 7
  • 47
  • 90
0

I found a tool named Avdshare Video Converter 7 that successfully concatenates my videos. It even transcodes them to just about any format I want.

I tried every solution on this page... all of them gave me the same errors you likely encountered if you're reading my answer. Does "Protocol not found" get you upset? Them maybe this is the program for you too.

I'm running it on windows.

The steps to concatenate two videos in the program are:

Drag and drop your videos onto the program

Click Merge

Choose a "save profile" that specifies every parameter you could ever want to tweak

Click Save Button

Mike from PSG
  • 5,312
  • 21
  • 39
0

After no getting an answer to the error message I was having with all of these similar answers:

~ avconv -i 'concat:' -c copy test.mp4
avconv version 12.3, Copyright (c) 2000-2018 the Libav developers
  built on Jan 22 2019 21:47:11 with Apple LLVM version 10.0.0 (clang-1000.11.45.5)
concat:: Protocol not found

I dug through some Google results, went into the bug tracker, and found these:

Seems it was not only disabled at some point, but removed in version 12, but my build 12.3 on Mac 10.13.6 from Homebrew still has "concat" listed in the input protocols, and I see it in the documentation as well, thus that 1 outstanding bug report.

I've had some luck using this command:

cat *.VOB | avconv -i - -c copy test.mp4
Pysis
  • 1,452
  • 2
  • 17
  • 31