Nov 19

How to merge videos with avconv or ffmpeg

Category: Linux,multimedia,Ubuntu,WIP   — Published by goeszen on November 19, 2015 at 5:59 pm

Ffmpeg and it's guise avconv are the swiss army knife of video transcoding and handling. We've already looked at editing out segments from a video using avconv, using it for simple "subtractive" video editing. Here now, we're specifically looking at concatenating or merging a number of video files into one continuous video, "additive video editing" so to speak. This article here takes a look at it and offers solutions (wip) for two specific use cases:

Merging video chunks recorded by a DVB TV receiver

nautilus showing a series of .ts files, as recorded by a DVB receiver

nautilus showing a series of .ts files, as recorded by a DVB receiver


Some (HD) DVB TV (DVB-S2, here, with a satellite dish) receivers produce a series of 1GB files when you record television programmes with them. That's probably because most USB-Sticks or other storage media connected to such receivers like external hard drives, are usually (pre-)formatted in FAT32, with all the FAT file size limitations. Also, handling smaller chunks might be easier, for the receivers OS, and in general.

But when you want to have a single file for any recording, and if your file-system doesn't care about maximum file sizes, these files, and how they are arranged (mostly, in subdirs, arg!) are quite annoying. Now, how do you get them back to be one single file. Or in general, how can you use avconv to edit/merge/concatenate videos?

The good news, these recordings are all MPEG-TS (MPEG transport stream) files, no headers, only a stream of video data.
So you don't need avconv to concat these files, not at all! Simply use cat and make them one big file. And it's save to append the .mp4 file suffix. Example: $ cat video1.ts video2.ts > video.mp4

But then there's unneeded data contained. DVB-S2 transmits one stream of error correction data, now contained in the file and as your receiver wrote the stream as it came down from the satellite, it's in there. Also, you might want to get rid of the DVB Teletext stream and probably unneeded audio versions or subtitling. Sadly, each TV station has another mix of those streams and in different order, so your avconv -map options need to be adjusted for every TV station you've recorded.

Here's an example "stream-splicing" command:
$ cat *.ts > Recording.ts && date && avconv -fix_sub_duration -i Recording.ts -map 0:2 -vcodec copy -map 0:3 -acodec copy -map 0:6 -scodec dvdsub Recording.mp4 && rm ./0* ./info*.dvr ./Recording.ts

Another scenario: Merging video files in differing formats

If you are dealing with video files in different containers, different codecs and want to merge them, these links might have additional insight. You'll need to transcode everything (except one) to one common codec, and you'll have to do some re-muxing, and you'll have to deal with avconv's concat input filter. You'll probably be better off using a simple GUI than dealing with a slew of command line options.

http://stackoverflow.com/questions/18552901/how-to-merge-videos-by-avconv
http://blog.dustinkirkland.com/2014/07/scalable-parallel-video-transcoding-on.html
http://stackoverflow.com/questions/7333232/concatenate-two-mp4-files-using-ffmpeg
https://trac.ffmpeg.org/wiki/Concatenate
https://ffmpeg.org/faq.html#How-can-I-concatenate-video-files_003f

Note: This post is a WIP, and incomplete.

Leave a Reply

=