How to keep/force aspect ratio using ffmpeg

9,963

To fix aspect ratio that gets broken after reencoding, try these steps:

  1. use parameter -aspect to force aspect ratio without reencoding:

    ffmpeg -i input.mkv -c copy -aspect 16/9 output.mkv
    

    Alternatively you can use the MKVmerge to do the same:

    mkvmerge --aspect-ratio 16:9 -o output.mkv input.mkv
    
  2. If soft aspect ratio does not help, you can force it in the reencoding by setting scale and setdar (set display aspect ratio) video filters:

    ffmpeg -i input.mkv -c copy c:v your_codec -vf "scale=1920:1080,setdar=16/9" output.mkv
    
  3. If even the scale filter does not help, you can reset the video by creating AVS script and then reencoding the file from the script. This way you will keep only the raw video bitstream without any metadata that may consufe the players to use incorrect aspect ratio.

    ffmpeg -i input.avs -c copy c:v your_codec output.mkv
    

    You need a program that will index the original video and then create the AVS file (e.g. on Windows you can use MeGUI and its Tools - File Indexer). You also need correct AviSynth and FFMPEG versions (32/64-bit) to work together.

    Here is example of an AVS file:

    LoadPlugin("/path/to/avisynth/ffms2.dll")
    FFVideoSource("/path/to/input.mkv", fpsnum=24000, fpsden=1001, threads=1, colorspace="YUV420P8")
    LanczosResize(1920,1080)
    
Share:
9,963

Related videos on Youtube

Widarma Putra
Author by

Widarma Putra

Updated on September 18, 2022

Comments

  • Widarma Putra
    Widarma Putra almost 2 years

    I would like to remove pt-br audio and convert english DTS to AC3 and I'm using this command:

    ffmpeg -i input.mkv -c:v copy -c:s copy -c:a ac3 -ac 2 -map 0:0 -map 0:2 -map 0:3 -aspect 16:9 test.mkv
    
    ffmpeg version N-63968-g73d820e Copyright (c) 2000-2014 the FFmpeg developers
      built on Jun 15 2014 00:52:21 with gcc 4.8.3 (GCC)
      configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-decklink --enable-zlib
      libavutil      52. 89.100 / 52. 89.100
      libavcodec     55. 66.101 / 55. 66.101
      libavformat    55. 43.100 / 55. 43.100
      libavdevice    55. 13.101 / 55. 13.101
      libavfilter     4.  8.100 /  4.  8.100
      libswscale      2.  6.100 /  2.  6.100
      libswresample   0. 19.100 /  0. 19.100
      libpostproc    52.  3.100 / 52.  3.100
    [matroska,webm @ 0000000002c480c0] Could not find codec parameters for stream 3 (Subtitle: hdmv_pgs_subtitle): unspecified size
    Consider increasing the value for the 'analyzeduration' and 'probesize' options
    Input #0, matroska,webm, from 'input.mkv':
      Metadata:
        encoder         : libebml v1.3.0 + libmatroska v1.4.0
        creation_time   : 2013-09-16 21:13:05
      Duration: 01:22:52.47, start: 0.000000, bitrate: 9307 kb/s
        Chapter #0.0: start 0.000000, end 137.846000
        Metadata:
          title           : 00:00:00.000
        Chapter #0.1: start 137.846000, end 227.185000
        Metadata:
          title           : 00:02:17.846
        Chapter #0.2: start 227.185000, end 351.643000
        Metadata:
          title           : 00:03:47.185
        Chapter #0.3: start 351.643000, end 536.035000
        Metadata:
          title           : 00:05:51.643
        Chapter #0.4: start 536.035000, end 648.982000
        Metadata:
          title           : 00:08:56.035
        Chapter #0.5: start 648.982000, end 901.359000
        Metadata:
          title           : 00:10:48.982
        Chapter #0.6: start 901.359000, end 1144.685000
        Metadata:
          title           : 00:15:01.359
        Chapter #0.7: start 1144.685000, end 1338.462000
        Metadata:
          title           : 00:19:04.685
        Chapter #0.8: start 1338.462000, end 1515.723000
        Metadata:
          title           : 00:22:18.462
        Chapter #0.9: start 1515.723000, end 1746.244000
        Metadata:
          title           : 00:25:15.723
        Chapter #0.10: start 1746.244000, end 1976.057000
        Metadata:
          title           : 00:29:06.244
        Chapter #0.11: start 1976.057000, end 2061.476000
        Metadata:
          title           : 00:32:56.057
        Chapter #0.12: start 2061.476000, end 2329.160000
        Metadata:
          title           : 00:34:21.476
        Chapter #0.13: start 2329.160000, end 2400.690000
        Metadata:
          title           : 00:38:49.160
        Chapter #0.14: start 2400.690000, end 2498.413000
        Chapter #0.15: start 2498.413000, end 2742.615000
        Metadata:
          title           : 00:41:38.413
        Chapter #0.16: start 2742.615000, end 3057.054000
        Metadata:
          title           : 00:45:42.615
        Chapter #0.17: start 3057.054000, end 3142.556000
        Metadata:
          title           : 00:50:57.054
        Chapter #0.18: start 3142.556000, end 3441.772000
        Metadata:
          title           : 00:52:22.556
        Chapter #0.19: start 3441.772000, end 3575.947000
        Metadata:
          title           : 00:57:21.772
        Chapter #0.20: start 3575.947000, end 3749.204000
        Metadata:
          title           : 00:59:35.947
        Chapter #0.21: start 3749.204000, end 3945.650000
        Metadata:
          title           : 01:02:29.204
        Chapter #0.22: start 3945.650000, end 4204.742000
        Metadata:
          title           : 01:05:45.650
        Chapter #0.23: start 4204.742000, end 4368.739000
        Metadata:
          title           : 01:10:04.742
        Chapter #0.24: start 4368.739000, end 4533.154000
        Metadata:
          title           : 01:12:48.739
        Chapter #0.25: start 4533.154000, end 4699.403000
        Metadata:
          title           : 01:15:33.154
        Chapter #0.26: start 4699.403000, end 4972.468000
        Metadata:
          title           : 01:18:19.403
        Stream #0:0(eng): Video: h264 (High), yuv420p, 1920x1080, SAR 1:1 DAR 16:9, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
        Metadata:
          title           : input
          stereo_mode     : left_right
        Stream #0:1(por): Audio: ac3, 48000 Hz, 5.1(side), fltp, 256 kb/s
        Metadata:
          title           : PT-BR: CAPS
        Stream #0:2(eng): Audio: dts (DTS), 48000 Hz, 5.1(side), fltp, 1536 kb/s (default)
        Metadata:
          title           : ENG
        Stream #0:3(por): Subtitle: hdmv_pgs_subtitle
        Metadata:
          title           : PT-BR
    Overriding aspect ratio with stream copy may produce invalid files
    Output #0, matroska, to 'test.mkv':
      Metadata:
        encoder         : Lavf55.43.100
        Chapter #0.0: start 0.000000, end 137.846000
        Metadata:
          title           : 00:00:00.000
        Chapter #0.1: start 137.846000, end 227.185000
        Metadata:
          title           : 00:02:17.846
        Chapter #0.2: start 227.185000, end 351.643000
        Metadata:
          title           : 00:03:47.185
        Chapter #0.3: start 351.643000, end 536.035000
        Chapter #0.4: start 536.035000, end 648.982000
        Metadata:
          title           : 00:08:56.035
        Chapter #0.5: start 648.982000, end 901.359000
        Metadata:
          title           : 00:10:48.982
        Chapter #0.6: start 901.359000, end 1144.685000
        Metadata:
          title           : 00:15:01.359
        Chapter #0.7: start 1144.685000, end 1338.462000
        Metadata:
          title           : 00:19:04.685
        Chapter #0.8: start 1338.462000, end 1515.723000
        Metadata:
          title           : 00:22:18.462
        Chapter #0.9: start 1515.723000, end 1746.244000
        Metadata:
          title           : 00:25:15.723
        Chapter #0.10: start 1746.244000, end 1976.057000
        Metadata:
          title           : 00:29:06.244
        Chapter #0.11: start 1976.057000, end 2061.476000
        Metadata:
          title           : 00:32:56.057
        Chapter #0.12: start 2061.476000, end 2329.160000
        Metadata:
          title           : 00:34:21.476
        Chapter #0.13: start 2329.160000, end 2400.690000
        Metadata:
          title           : 00:38:49.160
        Chapter #0.14: start 2400.690000, end 2498.413000
        Metadata:
          title           : 00:40:00.690
        Chapter #0.15: start 2498.413000, end 2742.615000
        Metadata:
          title           : 00:41:38.413
        Chapter #0.16: start 2742.615000, end 3057.054000
        Metadata:
          title           : 00:45:42.615
        Chapter #0.17: start 3057.054000, end 3142.556000
        Metadata:
          title           : 00:50:57.054
        Chapter #0.18: start 3142.556000, end 3441.772000
        Metadata:
          title           : 00:52:22.556
        Chapter #0.19: start 3441.772000, end 3575.947000
        Metadata:
          title           : 00:57:21.772
        Chapter #0.20: start 3575.947000, end 3749.204000
        Metadata:
          title           : 00:59:35.947
        Chapter #0.21: start 3749.204000, end 3945.650000
        Metadata:
          title           : 01:02:29.204
        Chapter #0.22: start 3945.650000, end 4204.742000
        Metadata:
          title           : 01:05:45.650
        Chapter #0.23: start 4204.742000, end 4368.739000
        Metadata:
          title           : 01:10:04.742
        Chapter #0.24: start 4368.739000, end 4533.154000
        Metadata:
          title           : 01:12:48.739
        Chapter #0.25: start 4533.154000, end 4699.403000
        Metadata:
          title           : 01:15:33.154
        Chapter #0.26: start 4699.403000, end 4972.468000
        Metadata:
          title           : 01:18:19.403
        Stream #0:0(eng): Video: h264 (H264 / 0x34363248), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 23.98 fps, 1k tbn, 1k tbc (default)
        Metadata:
          title           : input
          stereo_mode     : left_right
        Stream #0:1(eng): Audio: ac3 ([0] [0][0] / 0x2000), 48000 Hz, stereo, fltp, 192 kb/s (default)
        Metadata:
          title           : ENG
          encoder         : Lavc55.66.101 ac3
        Stream #0:2(por): Subtitle: hdmv_pgs_subtitle
        Metadata:
          title           : PT-BR
    Stream mapping:
      Stream #0:0 -> #0:0 (copy)
      Stream #0:2 -> #0:1 (dts (dca) -> ac3 (ac3))
      Stream #0:3 -> #0:2 (copy)
    Press [q] to stop, [?] for help
    [dca @ 0000000004f7d3a0] Overread auxiliary data by 32 bits
    [dca @ 0000000004f7d3a0] error decoding block
    Error while decoding stream #0:2: Invalid data found when processing input
    [dca @ 0000000004f7d3a0] Overread auxiliary data by 32 bits4 bitrate=7071.6kbits/s
    [dca @ 0000000004f7d3a0] error decoding block
    Error while decoding stream #0:2: Invalid data found when processing input
    [dca @ 0000000004f7d3a0] Overread auxiliary data by 32 bits5 bitrate=7762.9kbits/s
    [dca @ 0000000004f7d3a0] error decoding block
    Error while decoding stream #0:2: Invalid data found when processing input
    [dca @ 0000000004f7d3a0] Overread auxiliary data by 32 bits2 bitrate=8015.3kbits/s
    [dca @ 0000000004f7d3a0] error decoding block
    Error while decoding stream #0:2: Invalid data found when processing input
    [dca @ 0000000004f7d3a0] Overread auxiliary data by 32 bits3 bitrate=7842.7kbits/s
    [dca @ 0000000004f7d3a0] error decoding block
    Error while decoding stream #0:2: Invalid data found when processing input
    frame=119220 fps=961 q=-1.0 Lsize= 4708393kB time=01:22:52.34 bitrate=7757.1kbits/s
    video:4571784kB audio:116470kB subtitle:18062kB other streams:0kB global headers:0kB muxing overhead: 0.044125%
    

    Then running ffmpeg -i test.mkv (this is the output file from the above command) shows the following info about the aspect ratio:

    Stream #0:0(eng): Video: h264 (High), yuv420p, 1920x1080, SAR 1:2 DAR 8:9, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
    

    Look at the SAR/DAR value on stream 0:0. It's different from original file. I tried with and without -aspect parameter. I tried using -vf "DAR=16:9" but I get the message:

    Filtergraph 'dar=16:9' was defined for video output stream 0:0 but codec copy was selected. Filtering and streamcopy cannot be used together."
    

    How do I keep the aspect ratio without reencode the video?

    • Elisa Cha Cha
      Elisa Cha Cha over 9 years
      I am unable to duplicate this issue. Please show the complete ffmpeg console output from your command.
    • Widarma Putra
      Widarma Putra over 9 years
      Hi @LordNeckbeard. I've put full output log. Thank you.
    • Elisa Cha Cha
      Elisa Cha Cha over 9 years
      Is the new console output from the command above, or from a different command?
    • Widarma Putra
      Widarma Putra over 9 years
      I used the command: ffmpeg.exe -i input.mkv -ss 00:00:01 -t 00:00:31 -c copy test.mkv || And even with this command the video loses aspect. Sorry my poor english.
    • Widarma Putra
      Widarma Putra over 9 years
      I reencoded the video yesterday and even reencoding I can't keep the aspect radio. I tried using -aspect 16:9. Today I'll try using -vf "sar=1:1,dar=16:9"
  • DavidPostill
    DavidPostill almost 9 years
    Super User is an English-only site, and all the questions and answers must be in English.
  • DavidPostill
    DavidPostill almost 9 years
    While this may answer the question, it would be a better answer if you could provide some explanation why it does so.