Klaus Knopper answers your Linux questions

Joining Recorded Video Files

I have a digital satellite/DVB-C/S receiver, which through its PVR capability can record movies to USB flash disk or SD card. But, all recordings are split into several files ending in .ts or .mpg of about 1GB size. I can play each individual file with MPlayer or VLC with perfect audio/video synchronization, but if I concatenate them into a single file, suddenly audio is delayed by almost a second, which makes it difficult to watch the recorded movie.

  1. Why is that?
  2. How can I join the recorded files into a single one with perfect sync? Is there a good command line to do this?
  3. How can I watch the recorded movie on a smartphone or tablet with the included movie player, which apparently likes to play MP4 files rather than MPEG files?

Answer

To answer your first question, PVR-recorded movies are split into several files because the filesystem used often does not handle files larger than 4GB, and sometimes adding more data to a large file is slower than creating new files due to fragmentation or filesystem implementation issues. TS and MPG are common formats and file suffixes for MPEG video/audio streams [2]; they contain video and audio in a so-called "muxed" format.

When splitting the stream into files, audio and video are kept in sync by adding timestamps so that players know how to delay the audio or video part correctly. However, in most cases, the split parts of a stream do not contain perfectly synchronized chunks of audio and video, so players will add "silence" (or blank video frames) at the beginning or end of one stream that ends before the other. If you just concatenate the files with

cat *.ts > /path/to/video_library/movie.ts

the "missing" audio or video data slices at the end of each file (which are in fact continued in the next file) will erroneously get "filled in" by the player or video editing software, so the resulting file gets increasingly out of sync toward the end of the movie. Sometimes you are lucky, and the "gaps" are small enough not to notice, but sometimes you can get delays up to four seconds during a two-hour movie.

With regard to your second question, apart from using more "intelligent" video editing software that is prepared for resyncing the gaps between video/audio chunks, I found a good command line for FFmpeg. This will basically "demux" the movie (i.e., separate the video from the audio), concatenate audio and video separately, and then "remux" (put together) both tracks to a single file again:

ffmpeg -ss 00:00:00.2 -i "concat:000.ts|001.ts|002.ts" -i "concat:000.ts|001.ts|002.ts" \
  -acodec copy -vcodec copy -map 0:0 -map 1:1 -f mpegts /path/to/video_library/movie.ts

In this example, -ss 00:00:00.2 skips the first two frames (I noticed that some recorders write a nonsense first frame with invalid metadata, so it's better not to start at the first video frame).

The option -i "concat:000.ts|001.ts|002.ts" then concatenates three MPEG transport stream files from the recorded data and uses them as input (adjust this to match the real number of files). It is correct that this statement appears identically two times on the command line, because you want to extract the audio and video parts separately!

-acodec copy -vcodec copy will do a lossless copy of the audio and video stream with no conversion. Usually, the audio stream is MP2 (not MP3) or AC3, and the video stream is mpeg2video, which remains unchanged for the output.

-map 0:0 -map 1:1 interprets stream   from the first -i files as the video stream and stream 1 from the second -i files as the audio stream. This is the most common case; however, if your files have a different sequence of audio/video data streams, or several (multiple languages) audio streams, you may have to adjust the numbers after the 0: and 1: prefixes.

Finally, -f mpegts means to keep the MPEG transport stream file format in the output file, which is /path/to/video_library/movie.ts in the last statement.

The resulting file (movie.ts) can still have an offset between audio and video, but now at least it is a constant one that can easily be eliminated by, for example, adding an "audio delay" for the entire movie with the Avidemux video editor (Figure 3). Avidemux is a very basic and simple video editor that allows cutting ads and commercials out of a movie and saving the result in different formats.

Figure 3: Avidemux lets you add audio delay.

To answer your third question, this FFmpeg command line, resulting from many experiments, seems to obtain very good quality and best compatibility with a variety of MP4 video players on smartphones and tablets when converting from TS or VOB (DVD) video format to MP4 with H264 video encoding and AAC audio encoding:

ffmpeg -i movie.ts -f mp4 -vcodec libx264 -crf 20 -preset slow -tune film -strict experimental -ac 2 movie.mp4

You may want to experiment with keeping the unchanged original audio track for maximum audio quality by removing the -strict experimental and -ac 2 options and adding -acodec copy instead.

Afterward, you can copy the resulting .mp4 file to the SD card of the smartphone or tablet, or to the local UPnP/DLNA streaming storage server you use with your mobile devices.

Note that the offline encoding with FFmpeg shown above is quite slow, even with fast CPUs, but it reduces the file size to at least 50 percent of the original MPEG2 stream while maintaining good quality. Proprietary as well as free (VLC, MPlayer) video players will most likely be able to decode the H264 codec in real time using the hardware decoder integrated in the graphics chipset.

If you have no hardware decoder and a slow CPU on your mobile device, you may need to reduce the movie's frame rate by adding a flag like -r 15 (which is only 15 frames/second instead of the usual 25).

The Author

Klaus Knopper is the creator of Knoppix and co-founder of LinuxTag expo. He currently is a Professor, Dipl. Ing., at the University of Applied Sciences Kaiserslautern. If you have a configuration problem, or if you just want to learn more about how Linux works, send your questions to: mailto:klaus@linux-magazine.com.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Knoppix 7.3

    Knoppix 7.3 comprises the current state of Debian GNU/Linux development and comes with the current hardware support of kernel 3.13, a new update function, and extended security and privacy features.

  • Practical Knoppix

    Knoppix creator Klaus Knopper shares some tips for using Knoppix in the real world.

  • Ask Klaus!

    Strategies for getting around flash drive limitations and updating OSs on flash drives.

  • Ask Klaus!

    Klaus addresses problems with Wheezy updates, Adobe Flash, dial-up settings, and mountpoint dates.

  • Ask Klaus!

    Klaus Knopper is the creator of Knoppix and co-founder of LinuxTag expo. He currently works as a teacher, programmer, and consultant. If you have a configuration problem, or if you just want to learn more about how Linux works, send your questions to: klaus@linux-magazine.com

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News