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
-
First Release Candidate for Linux Kernel 6.14 Now Available
Linus Torvalds has officially released the first release candidate for kernel 6.14 and it includes over 500,000 lines of modified code, making for a small release.
-
System76 Refreshes Meerkat Mini PC
If you're looking for a small form factor PC powered by Linux, System76 has exactly what you need in the Meerkat mini PC.
-
Gnome 48 Alpha Ready for Testing
The latest Gnome desktop alpha is now available with plenty of new features and improvements.
-
Wine 10 Includes Plenty to Excite Users
With its latest release, Wine has the usual crop of bug fixes and improvements, along with some exciting new features.
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.