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.
- Why is that?
- How can I join the recorded files into a single one with perfect sync? Is there a good command line to do this?
- 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.
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).
Infos
- Heartbleed: http://en.wikipedia.org/wiki/Heartbleed
- Transport Stream: http://en.wikipedia.org/wiki/Transport_Stream
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.