Fixing the audio on recorded programs from a certain network (which shall remain nameless)

This bird has no relevance to anything in this article, I just thought it was a nice picture (Source: Public Domain Pictures.net)

This bird has no relevance to anything in this article, I just thought it was a nice picture 😉 (Source: Public Domain Pictures.net)

I have said that I won’t mention any specific channels or networks you can receive in North America by name (with specific exceptions), but if you receive channels from either the Ku-band side of AMC-1 or the C-band side of AMC-18 you have probably encountered some channels with split up audio, where various channels are sent on different streams. The most common format on those channels seems to be:

Stream 1: Left and right front
Stream 2: Center (on left channel) and low frequency (on right channel)
Stream 3: Left and right rear
Stream 4: Described video (when used, on left) and mono (on right channel)

If you have recorded a program from one of those channels as a .ts file, then ffmpeg can convert it to real 5.1 audio, keeping all other streams including video as is, and usually in under five minutes unless you have a underpowered computer:

In FFMPEG Version 2.x:
ffmpeg -i "original program filename.ts" -c copy -filter_complex '[0:1][0:2][0:3]amerge=inputs=3,pan=5.1|FL=c0|FR=c1|FC=c2|LFE=c3|BL=c4|BR=c5' -c:a eac3 "converted program filename.ts"

In FFMPEG Version 3.0 and later (only when using eac3 as the output codec):
ffmpeg -i "original program filename.ts" -c copy -filter_complex '[0:1][0:2][0:3]amerge=inputs=3,pan=5.1|FL=c0|FR=c1|FC=c2|LFE=c3|BL=c4|BR=c5' -c:a eac3 -mpegts_flags system_b "converted program filename.ts"

The above is all one line, even if it appears otherwise in this blog. This discards stream 4, and maps the other three to 5.1 audio. Note that this converts the audio to eac3, which Kodi will generally play as true 5.1 audio, depending on its audio settings and the ability of your AV receiver to handle encoded audio. If that doesn’t work for some reason, you can try changing the -c:a option from eac3 to ac3 or mp2 (and omit the -mpegts_flags system_b option if using ffmpeg version 3.0 or later, since that option is only needed when using the eac3 codec), but in my experience Kodi treats mp2 audio as stereo only.

Note that although the above example lines attempt to copy all non-audio streams verbatim, including ATSC closed captions, I have noticed that closed captions don’t fare well after passing through ffmpeg. In some cases they are missing altogether, and in other cases they are just garbled or missing characters. I don’t know why this happens and I don’t have any solution for the problem at this time.

Some backend programs, such as TVHeadEnd, give you the option to run a Post-Processor Command when a recording is finished, so with such a backend it may be possible to do this conversion automatically. Note, however, that ffmpeg by default sends a lot of output to stderr, which can cause issues in some systems (or just send a lot of unnecessary email to the system administrator), particularly if you are trying to run ffmpeg from a script. Once you know you have it working, you can suppress this output by using the -loglevel quiet switch, like this:

In FFMPEG Version 2.x:
ffmpeg -loglevel quiet -i "original program filename.ts" -c copy -filter_complex '[0:1][0:2][0:3]amerge=inputs=3,pan=5.1|FL=c0|FR=c1|FC=c2|LFE=c3|BL=c4|BR=c5' -c:a eac3 "converted program filename.ts"

In FFMPEG Version 3.0 and later (only when using eac3 as the output codec):
ffmpeg -loglevel quiet -i "original program filename.ts" -c copy -filter_complex '[0:1][0:2][0:3]amerge=inputs=3,pan=5.1|FL=c0|FR=c1|FC=c2|LFE=c3|BL=c4|BR=c5' -c:a eac3 -mpegts_flags system_b "converted program filename.ts"

If you would like to have logging, but only when something goes terribly wrong, use -loglevel fatal instead of -loglevel quiet.

Note: For this to work, you must have a version of ffmpeg from ffmpeg.org (link is to download page) or, if you are a Linux user but do not feel comfortable attempting to build software from source, then you may be better off using a static build such as is offered at this site (technically this is an unofficial build, so use at your own risk, but as I write this the packages offered in the deb repositories are older versions that don’t work as well for this purpose, and produce files that don’t play well on underpowered systems). Please note that some Linux distributions, notably some versions of Debian and Ubuntu and their derivatives, provide a package called ffmpeg in their repositories that is not true ffmpeg, but instead is a transitional package for libav. The syntax shown above will not work with the transitional package version; you need true ffmpeg for this to work! For the reason this sad situation exists, see The FFmpeg/Libav situation or for a slightly more technical view, FFmpeg versus Libav.

Another possible way to get the real ffmpeg installed on Ubuntu and some derivative systems is explained in the article, How To Install FFmpeg 2.6.1 On Ubuntu 15.04, Ubuntu 14.10, Ubuntu 14.04 And Derivative Systems from the LinuxG.net site.

I am not adverse to libav or a companion package called avconv for this process, I just don’t know if either of those can be used for this purpose, or if so, what the correct syntax would be. If anyone knows, please feel free to leave a comment.

There is a followup to this article: Fixing the audio on live TV from a certain network (which shall remain nameless) in TVHeadend. This discusses how to do the audio conversion in real time, so that you can view live TV with proper audio, or begin watching a recorded program before it has completely finished recording.

You may also be interested in this article:
How to play video recorded from high-bitrate 4:2:2 sources on low-power systems

Advertisements

One thought on “Fixing the audio on recorded programs from a certain network (which shall remain nameless)

  1. Those wanting to do this conversion in real time in TVHeadEnd might find this post interesting. It’s an adaptation of what you show here, but used to convert a stream in real time. The downside is that according to the person who posted it, you need to have TVHeadEnd “built from git within the past few months” for some reason, which means this may not work for most users.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s