Ffmpeg – adding/re-encoding audio stream on video file

audioffmpegstreamingvideo

I want to add an audio stream to an existing video file. The issue I have is that:

  • File can have more than 1 audio stream (different languages/commentary).
  • File can have more than 1 text stream.

My problem is that sometimes the audio codec is not compatible with my media player, so I simply want to copy "X" audio stream (usually DTS), convert it to AAC and add it to the same file.

I've tried some examples from this forum but I've mixed results. Especially because of the txt streams.
I'm sure there's a way of getting this output in one go.

Is this the right command?

ffmpeg -i "input-video.mkv" -map 0:v -c:v copy -map 0:a -c:a copy -map 0:1 -c:1 libfdk_aac -b:a 384k -map 0:s -c:s copy "output-video.mkv"

Where:

-map 0:1 -c:1 libfdk_aac -b:a 384k

Means map file 0, stream1, copy… and then I don't know what it's doing

I have also added output of ffmpeg -i "input-video.mkv"

Input #0, matroska,webm, from 'input-video.mkv':
  Metadata:
    title           : input-video
    encoder         : libebml v0.7.7 + libmatroska v0.8.1
    creation_time   : 2008-09-18T20:25:36.000000Z
  Duration: 02:23:27.34, start: 0.000000, bitrate: 12303 kb/s
    Stream #0:0(eng): Video: h264 (High), yuv420p(progressive), 1920x800, SAR 1:1 DAR 12:5, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
    Stream #0:1(eng): Audio: dts (DTS), 48000 Hz, 5.1(side), fltp, 1536 kb/s (default)
    Stream #0:2(eng): Audio: vorbis, 48000 Hz, stereo, fltp
    Metadata:
      title           : Commentary
    Stream #0:3(eng): Subtitle: ass (default)
    Stream #0:4(eng): Subtitle: ass
    Metadata:
      title           : Commentary
    Stream #0:5(cze): Subtitle: ass
    Stream #0:6(dan): Subtitle: ass
    Stream #0:7(fre): Subtitle: ass
    Stream #0:8(ger): Subtitle: ass
    Stream #0:9(hun): Subtitle: ass
    Stream #0:10(nor): Subtitle: ass
    Stream #0:11(pol): Subtitle: ass
    Stream #0:12(por): Subtitle: ass
    Stream #0:13(rus): Subtitle: ass
    Stream #0:14(scc): Subtitle: ass
    Stream #0:15(scr): Subtitle: ass
    Stream #0:16(slv): Subtitle: ass
    Stream #0:17(spa): Subtitle: ass
    Stream #0:18(swe): Subtitle: ass

Best Answer

Assuming you want two copies of the audio stream, one as original DTS, and one as AAC, map the stream meant for re-encode first.

ffmpeg -i "input-video.mkv" -map 0:v -map 0:a:0 -map 0:a -map 0:s -c copy -c:a:0 libfdk_aac -b:a:0 384k "output-video.mkv"

Related Question