Skip to content

Latest commit

 

History

History
118 lines (95 loc) · 4.41 KB

audio_video.md

File metadata and controls

118 lines (95 loc) · 4.41 KB

Audio / Video

Downsampling 7.1 AAC Audio Track to 5.1 AC3 (w/o Touching Video)

Prelude: Completely Needless Rant about Media Players

All media players are pieces of shit. VLC freezes the video every time you take a deep breath. Windows Media Player ("Movies and TV" or however they call this garbage nowadays) cannot adapt audio streams to what the audio device can actually play, and instead of giving any information about this just goes the passive aggressive way and punishes you with deadly silence.

Kodi is nice and all, but you don't want to boot up a full-blown media center all the time.

Issue: Silence

Got some totally legal video with a fancy surround audio track, but can't hear shit (see above). FFmpeg's ffprobe reveals the audio track is 7.1 AAC. That's nice and all, but I only have a 5.1 setup from the middle ages, and (also see above) Windows' "Movies and TV" thingy won't adapt the audio for me (VLC does, but freezes the video on every prime number video frame).

Solution: Transcode the Audio

5.1 AC3 has worked for decades, so this should be good enough. FFmpeg can easily do the audio conversion for me, without touching the video or subtitle tracks, so it's also super fast:

ffmpeg -i legit_movie.mkv \
    -map 0 \                            # take all streams
    -vcodec copy -scodec copy \         # no touchy pls
    -acodec ac3 -b:a 640k \             # gimme sum audio I can hear, yo
    legit_movie_that_fucking_works.mkv

Alternative: On the Fly Transcoding with mpv

As suggested by @neverpanic, mpv can do the conversion on the fly, while playing the video. I always heard that mpv is pretty great, but I wasn't aware that it is available for Windows (there is even a chocolatey package), so I had not considered it so far.
Update: there is also mpv.net, which is even nicer and available via winget.

Turns out, it's pretty cool, and works just fine on Windows. After some struggles with its immense set of configuration options, I ended up with this config snippet that does the job:

# Let AC3 and DTS audio streams pass through to the A/V without touching.
# See: https://mpv.io/manual/master/#options-audio-spdif
audio-spdif=ac3,dts
#
# Here comes the magical part: *other* codecs (specifically DTS) get
# transcoded to AC3.
# See: https://mpv.io/manual/master/#audio-filters-lavcac3enc[
#
# scaletempo is needed to keep audio in sync when speeding up/down ([/]).
# Note that this does not work for audio streams directly passed through.
af=lavcac3enc,scaletempo

The options can either be passed directly when calling mpv (--audio-spdif=ac3,dts --af=lavcac3enc,scaletempo), or added as shown above to the config file (~/.config/mpv/mpv.conf on Lunix systems, %APPDATA%\mpv\mpv.conf on Windows (symlinking works)).

Converting a Video to Twitter Compatible Format

Might not be ideal, but has proven to work:

 ffmpeg \
      -i input.webm       \
      -acodec aac         \
      -vcodec libx264     \
      -profile:v baseline \
      -b:v 1000k          \
      -pix_fmt yuv420p    \
      output_that_works_for_twitter.mp4

Creating a Video from a Still Image and Audio

This is e.g. required for Twitter (or YouTube), where you cannot post plain audio files, but only video. So, we have to find some fitting still image, ideally resize it to a common video resolution like 1280×720, and use FFmpeg to create a video from it with the audio track.

Here is a command that takes a PNG and an MP3 as inputs, and creates an MP4 with AAC audio and h264 baseline video as outout (which seems to work on Twitter, Twitter is super picky about the media files it takes):

ffmpeg \
    -stream_loop -1     \
    -i pic.png          \
    -i sound.mp3        \
    -shortest           \
    -acodec aac         \
    -vcodec libx264     \
    -profile:v baseline \
    -b:v 1000k          \
    -pix_fmt yuv420p    \
    out.mp4

Explanations for non-obvious options:

  • -stream_loop -1 - loop the image infinitely
  • -shortest - finish after audio stream ends
  • -profile:v baseline - baseline profile for h264, so that Twitter is happy
  • -pix_fmt yuv420p - some color voodoo, might depend on input image