How to merge videos by avconv?
Asked Answered
R

10

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.

Rattray answered 31/8, 2013 at 21:15 Comment(4)
I've filed a related bug when it comes to webm here: bugzilla.libav.org/show_bug.cgi?id=775Gorga
There's a link rot problem, since you've deleted the avconv.txt file from your dropbox. I'm going to remove it from the question.Gorga
superuser.com/questions/521113/join-mp4-files-in-linuxAlden
I used avconv -i "concat:`ls *.mp4 | tr '\r\n' '\|'" -c copy "combined.mp4" on windows. For linux I think you'd just use \n, not \r\nSwinney
P
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 ).

Phebe answered 11/10, 2013 at 9:15 Comment(6)
Didnt work for me... had to do it with mencoder... mencoder -oac pcm -ovc copy -o merged_file.mp4 ls *.mp4Bartell
mp4 containers cannot be concatenated in this way.Voroshilovsk
mp4 and matroska cannot be concatenated. You need to recode into a concatenatable or randomly seekable container first, like MPEG-TS or maybe NUT.Lagging
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.mp4Deadhead
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.Asperity
As @Phebe stated it did, this technique worked for me with an avi wrapper.Bangtail
V
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:

Voroshilovsk answered 11/7, 2014 at 14:58 Comment(3)
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?Teishateixeira
What version of avconv do you have?Manful
I'm having the same problem with avconv version 12. Version 11 doesn't work eitherVair
P
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 ).

Phebe answered 11/10, 2013 at 9:15 Comment(6)
Didnt work for me... had to do it with mencoder... mencoder -oac pcm -ovc copy -o merged_file.mp4 ls *.mp4Bartell
mp4 containers cannot be concatenated in this way.Voroshilovsk
mp4 and matroska cannot be concatenated. You need to recode into a concatenatable or randomly seekable container first, like MPEG-TS or maybe NUT.Lagging
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.mp4Deadhead
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.Asperity
As @Phebe stated it did, this technique worked for me with an avi wrapper.Bangtail
B
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 :-(

Bartell answered 21/12, 2014 at 21:13 Comment(2)
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.Tail
A shorter command line that will take in all mp4 files in the current directory is: MP4Box $(printf -- "-cat %s " *.mp4) -new mergedFile.mp4Asperity
I
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
Ixion answered 19/10, 2014 at 15:58 Comment(2)
Shows a lot of errors (at the last step), but the only answer that does result in a merged file for me so +1!Hyoscyamus
This almost works for me, but with a loss of quality... (the image is pixelized.) Any way to solve this problem?Carrew
I
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.

Involucel answered 7/6, 2015 at 12:24 Comment(2)
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)Dapper
Got the above error too in the inputs list with avconv 12.3 on Mac 10.13.6 with Homebrew.Herra
C
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.

Congruity answered 1/10, 2014 at 22:16 Comment(0)
S
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 !

Statvolt answered 5/5, 2015 at 17:38 Comment(0)
H
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

Hayley answered 19/3, 2016 at 5:4 Comment(0)
C
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

Chemoreceptor answered 29/6, 2018 at 16:7 Comment(0)
H
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
Herra answered 24/6, 2019 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.