Ffmpeg: convert and keep audio track

audioconversionffmpeg

I have some video files in H.264 codec and DTS audio. My goal is to convert the audio tracks (mostly 2 tracks, English and German) to AAC, but also keep the DTS audio track.

I tried this command, but it only converts the tracks and replaces the DTS tracks with AAC.

$ ffmpeg -i input.mkv -map 0 -c:v copy -c:a libfdk_aac -b:a 128k \
  -c:s mov_text -t 30 output.mp4

ffmpeg version git-2014-09-08-5a6dd92 Copyright (c) 2000-2014 the FFmpeg develop
ers
  built on Sep  8 2014 18:19:34 with gcc 4.8.3 (GCC)
  configuration: --prefix=/usr/local/x86_64-w64-mingw32 --cross-prefix=x86_64-w6
4-mingw32- --target-os=mingw32 --enable-w32threads --enable-memalign-hack --arch
=x86_64 --enable-runtime-cpudetect --disable-debug --enable-static --disable-sha
red --disable-ffplay --disable-ffserver --enable-gpl --enable-version3 --enable-
nonfree --enable-libmp3lame --enable-libfdk-aac --enable-libx264 --extra-cxxflag
s='-I/usr/local/x86_64-w64-mingw32/include -march=native' --extra-cflags='-march
=native' --extra-ldflags='-L/usr/local/x86_64-w64-mingw32/lib -march=native' --c
pu=native
  libavutil      54.  7.100 / 54.  7.100
  libavcodec     56.  1.100 / 56.  1.100
  libavformat    56.  4.101 / 56.  4.101
  libavdevice    56.  0.100 / 56.  0.100
  libavfilter     5.  1.100 /  5.  1.100
  libswscale      3.  0.100 /  3.  0.100
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  0.100 / 53.  0.100
Input #0, matroska,webm, from 'input.mkv':
  Metadata:
    title           : input
    encoder         : libebml v1.2.2 + libmatroska v1.3.0
    creation_time   : 2013-09-29 09:30:40
  Duration: 01:34:11.94, start: 0.000000, bitrate: 12120 kb/s
    Stream #0:0(ger): Video: h264 (High), yuv420p(tv, bt709), 1920x1080 [SAR 1:1
 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default) (forced)
    Metadata:
      title           : input
    Stream #0:1(ger): Audio: dts (DTS), 48000 Hz, 5.1(side), fltp, 1536 kb/s (de
fault) (forced)
    Stream #0:2(eng): Audio: dts (DTS), 48000 Hz, 5.1(side), fltp, 1536 kb/s
    Stream #0:3(ger): Subtitle: subrip (default) (forced)
    Metadata:
      title           : Forced.Sub
File 'output.mp4' already exists. Overwrite ? [y/N] y
Output #0, mp4, to 'output.mp4':
  Metadata:
    title           : output
    encoder         : Lavf56.4.101
    Stream #0:0(ger): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1920x1080 [
SAR 1:1 DAR 16:9], q=2-31, 23.98 fps, 16k tbn, 1k tbc (default) (forced)
    Metadata:
      title           : output
    Stream #0:1(ger): Audio: aac (libfdk_aac) ([64][0][0][0] / 0x0040), 48000 Hz
, 5.1, s16, 128 kb/s (default) (forced)
    Metadata:
      encoder         : Lavc56.1.100 libfdk_aac
    Stream #0:2(eng): Audio: aac (libfdk_aac) ([64][0][0][0] / 0x0040), 48000 Hz
, 5.1, s16, 128 kb/s
    Metadata:
      encoder         : Lavc56.1.100 libfdk_aac
    Stream #0:3(ger): Subtitle: mov_text ([8][0][0][0] / 0x0008) (default) (forc
ed)
    Metadata:
      title           : Forced.Sub
      encoder         : Lavc56.1.100 mov_text
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (dts (dca) -> aac (libfdk_aac))
  Stream #0:2 -> #0:2 (dts (dca) -> aac (libfdk_aac))
  Stream #0:3 -> #0:3 (subrip (native) -> mov_text (native))
Press [q] to stop, [?] for help
frame=   34 fps=0.0 q=-1.0 size=       0kB time=00:00:01.49 bitrate=   0.3kbits/
frame=   68 fps= 68 q=-1.0 size=       0kB time=00:00:02.81 bitrate=   0.1kbits/
frame=   98 fps= 65 q=-1.0 size=       0kB time=00:00:04.13 bitrate=   0.1kbits/
frame=  129 fps= 64 q=-1.0 size=       0kB time=00:00:05.50 bitrate=   0.1kbits/
frame=  160 fps= 64 q=-1.0 size=       0kB time=00:00:06.82 bitrate=   0.1kbits/
frame=  193 fps= 64 q=-1.0 size=       0kB time=00:00:08.10 bitrate=   0.0kbits/
frame=  225 fps= 64 q=-1.0 size=       0kB time=00:00:09.42 bitrate=   0.0kbits/
frame=  255 fps= 64 q=-1.0 size=      71kB time=00:00:10.73 bitrate=  54.3kbits/
frame=  282 fps= 63 q=-1.0 size=     245kB time=00:00:11.83 bitrate= 169.6kbits/
frame=  315 fps= 63 q=-1.0 size=     422kB time=00:00:13.20 bitrate= 261.9kbits/
frame=  348 fps= 63 q=-1.0 size=     519kB time=00:00:14.50 bitrate= 293.1kbits/
frame=  376 fps= 63 q=-1.0 size=     632kB time=00:00:15.80 bitrate= 327.4kbits/
frame=  398 fps= 61 q=-1.0 size=     722kB time=00:00:16.57 bitrate= 356.6kbits/
frame=  424 fps= 61 q=-1.0 size=     892kB time=00:00:17.77 bitrate= 411.4kbits/
frame=  456 fps= 61 q=-1.0 size=    1012kB time=00:00:19.11 bitrate= 433.8kbits/
frame=  486 fps= 61 q=-1.0 size=    1076kB time=00:00:20.35 bitrate= 433.1kbits/
frame=  518 fps= 61 q=-1.0 size=    1141kB time=00:00:21.58 bitrate= 433.0kbits/
frame=  542 fps= 60 q=-1.0 size=    1199kB time=00:00:22.65 bitrate= 433.4kbits/
frame=  570 fps= 60 q=-1.0 size=    1273kB time=00:00:23.76 bitrate= 439.0kbits/
frame=  598 fps= 60 q=-1.0 size=    1317kB time=00:00:25.00 bitrate= 431.5kbits/
frame=  630 fps= 60 q=-1.0 size=    1362kB time=00:00:26.28 bitrate= 424.6kbits/
frame=  659 fps= 60 q=-1.0 size=    3275kB time=00:00:27.58 bitrate= 972.7kbits/
frame=  691 fps= 60 q=-1.0 size=    4524kB time=00:00:28.82 bitrate=1285.9kbits/
frame=  722 fps= 60 q=-1.0 size=    5548kB time=00:00:29.98 bitrate=1515.5kbits/
frame=  722 fps= 58 q=-1.0 size=    5548kB time=00:00:29.98 bitrate=1515.5kbits/
frame=  722 fps= 56 q=-1.0 size=    5548kB time=00:00:29.98 bitrate=1515.5kbits/
frame=  722 fps= 53 q=-1.0 size=    5548kB time=00:00:29.98 bitrate=1515.5kbits/
frame=  722 fps= 52 q=-1.0 size=    5548kB time=00:00:29.98 bitrate=1515.5kbits/
frame=  722 fps= 50 q=-1.0 size=    5548kB time=00:00:29.98 bitrate=1515.5kbits/
frame=  722 fps= 48 q=-1.0 size=    5548kB time=00:00:29.98 bitrate=1515.5kbits/
frame=  722 fps= 47 q=-1.0 size=    5548kB time=00:00:29.98 bitrate=1515.5kbits/
frame=  722 fps= 45 q=-1.0 size=    5548kB time=00:00:29.98 bitrate=1515.5kbits/
frame=  722 fps= 44 q=-1.0 size=    5548kB time=00:00:29.98 bitrate=1515.5kbits/
frame=  722 fps= 42 q=-1.0 size=    5548kB time=00:00:29.98 bitrate=1515.5kbits/
frame=  722 fps= 41 q=-1.0 size=    5548kB time=00:00:29.98 bitrate=1515.5kbits/
frame=  722 fps= 40 q=-1.0 Lsize=   24270kB time=00:00:30.01 bitrate=6623.9kbits
/s
video:23278kB audio:942kB subtitle:0kB other streams:0kB global headers:0kB muxi
ng overhead: 0.208187%

What should I do to have the DTS and AAC tracks in the output file?

Best Answer

  • You can use the -map and -codec options with stream specifiers to choose what streams you want and if you would like to stream copy or encode the streams:

    ffmpeg -i input.mkv \
    -map 0 -c copy \
    -c:s mov_text \
    -map 0:a:0 -c:a:0 libfdk_aac -b:a:0 128k \
    -map 0:a:1 -c:a:1 libfdk_aac -b:a:1 128k \
    output.mp4
    

    The mapping in this example goes from general to specific. At first all inputs are stream copied, but the later options refine the behavior so the subtitles are converted instead of copied and more maps are added to include the audio to be re-encoded.

  • Related Question