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
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.