如何使用 ffmpeg 保持/强制宽高比

如何使用 ffmpeg 保持/强制宽高比

我想删除 pt-br 音频并将英语 DTS 转换为 AC3,我使用此命令:

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%

然后运行ffmpeg -i test.mkv(这是上述命令的输出文件)显示有关纵横比的以下信息:

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)

查看流 0:0 上的 SAR/DAR 值。它与原始文件不同。我尝试了带参数和不带-aspect参数。我尝试使用-vf "DAR=16:9",但收到以下消息:

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

如何在不重新编码视频的情况下保持纵横比?

答案1

要修复重新编码后损坏的宽高比,请尝试以下步骤:

  1. 使用参数-aspect强制纵横比而不重新编码:

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

    或者,您可以使用 MKVmerge 执行相同操作:

    mkvmerge --aspect-ratio 16:9 -o output.mkv input.mkv
    
  2. 如果软纵横比没有帮助,您可以通过设置比例和 setdar (设置显示纵横比)视频过滤器来强制重新编码:

    ffmpeg -i input.mkv -c copy c:v your_codec -vf "scale=1920:1080,setdar=16/9" output.mkv
    
  3. 如果尺度过滤器也不起作用,你可以重置通过创建 AVS 脚本然后从脚本重新编码文件来压缩视频。这样,您将只保留原始视频比特流,而不会保留任何可能导致播放器使用不正确的宽高比的元数据。

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

    您需要一个程序来索引原始视频,然后创建 AVS 文件(例如在 Windows 上,您可以使用图形界面及其工具 - 文件索引器)。您还需要正确的 AviSynth 和 FFMPEG 版本(32/64 位)才能协同工作。

    以下是 AVS 文件的示例:

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

答案2

-地图 0:0 -地图 0:1 -地图 0:2
ffmpeg -i 输入.mkv -c:v 复制 -c:s 复制 -c:a ac3 -ac 2 -map 0:0 -map 0:1 -map 0:2 -aspect 16:9 测试.mkv

如果不起作用,请避免使用“复制”编辑转换。

答案3

我在尝试使用 yt-dlp(使用 ffmpeg 作为后处理器)时遇到了这个问题。有些视频似乎一开始的 SAR 就不对(在大多数情况下应该是 1:1,也就是方形像素),当我将视频转换为 h.264 时,它在不同的软件中显示不正确且不一致。这似乎已更正:

yt-dlp -f 313+140 --merge-output-format mp4 --postprocessor-args "ffmpeg:-c:v libx264 -c:a copy -vf scale=iw:ih,setsar=sar=1,setdar=dar=w/h" "https://www.youtube.com/watch?v=1234567"

在上面我使用了 FFmpeg 的视频滤镜(-vf),将比例设置为当前输入的宽度和高度(比例=iw:ih)设置样本/像素纵横比(特区)设置为 1,并设置显示宽高比(達爾) 与高度相同 (设置dar=dar=w/h)。如果我没有定义所有 3 个值,它只会在某些软件中正确显示。

相关内容