gpt4 book ai didi

ffmpeg - Python ffmpeg 在缩放文件大小方面的差异

转载 作者:行者123 更新时间:2023-12-04 23:18:33 27 4
gpt4 key购买 nike

def crop(input_file, output_file, x, y):
subprocess.call(['ffmpeg', '-i', input_file, '-filter:v', 'scale=.{}/{}*iw:ih'.format(x, y), output_file])

crop('C:/home/filename.m4v', 'C:/home/filename.mp4', 320, 240)
文件转换没问题,但文件大小比我的时候大了很多 ffmpeg -i filename.m4v -vf scale=320:240 filename.mp4
original file=3523
python=3765
command line=1

888
如果我使用 python 版本并使用命令行进行转换=1872
为什么会出现差异?我正在尝试更改文件大小,并且在大多数情况下希望节省磁盘空间。我该怎么做才能用 python 获得更小的文件大小?我有数百个要转换的视频,这将使它比使用命令行更自动化/更快。
编辑如下:
我决定尝试使用不同的视频,看看比较会是什么。
使用:
https://www.youtube.com/watch?v=T1tV-ovGPyc
我下载了 480 和 360 版本来比较原始文件大小并比较我调整大小的时间,无论是使用命令行还是使用 python。
480 = 215.924 MB
360 = 170.333 Mb
使用命令行转换时:
ffmpeg -i filename.mp4 scale=320:240 filename1.mp4
480 = 116.488 MB
360 = 115.454 MB
当我使用 python 时:
subprocess.call(['ffmpeg', '-i', input_file, '-crf:23', 'scale=.{}/{}*iw:ih'.format(x, y), output_file])
480 = 264.509 MB
360 = 193.640 MB
将其更改为 -crf:50 基于 python:
480 = 264.509 MB
360 = 193.640 MB
不确定我是否可以尝试将命令行更改为 -crf:50 并将比例保持在 320:240。我的快速尝试不断给我一个错误'无法找到适合'scale = 320:240'的输出格式'
这是使用命令行时的完整设置/结束日志(上面提到的 360 文件)
ffmpeg version N-105780-g0d0002cd20-20220227 Copyright (c) 2000-2022 the FFmpeg developers

built with gcc 11.2.0 (crosstool-NG 1.24.0.533_681aaef)

configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --disable-w32threads --enable-pthreads --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvorbis --enable-opencl --disable-libpulse --enable-libvmaf --disable-libxcb --disable-xlib --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-libdavs2 --disable-libfdk-aac --enable-ffnvcodec --enable-cuda-llvm --enable-frei0r --enable-libgme --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librist --enable-libtheora --enable-libvpx --enable-libwebp --enable-lv2 --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopenmpt --enable-librav1e --enable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --disable-libdrm --disable-vaapi --enable-libvidstab --enable-vulkan --enable-libshaderc --enable-libplacebo --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libzimg --enable-libzvbi --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-ldexeflags= --extra-libs=-lgomp --extra-version=20220227

libavutil 57. 21.100 / 57. 21.100

libavcodec 59. 21.100 / 59. 21.100

libavformat 59. 17.102 / 59. 17.102

libavdevice 59. 5.100 / 59. 5.100

libavfilter 8. 27.100 / 8. 27.100

libswscale 6. 5.100 / 6. 5.100

libswresample 4. 4.100 / 4. 4.100

libpostproc 56. 4.100 / 56. 4.100

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'filename.mp4':

Metadata:

major_brand : mp42

minor_version : 0

compatible_brands: isommp42

creation_time : 2018-06-06T01:11:10.000000Z

Duration: 00:42:39.05, start: 0.000000, bitrate: 544 kb/s

Stream #0:0[0x1](und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p(tv, smpte170m/smpte170m/bt709, progressive), 540x360 [SAR 1:1 DAR 3:2], 446 kb/s, 29.97 fps, 29.97 tbr, 30k tbn (default)

Metadata:

creation_time : 2018-06-06T01:11:10.000000Z

handler_name : ISO Media file produced by Google Inc.

Created on: 06/05/2018.

vendor_id : [0][0][0][0]

Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 95 kb/s (default)

Metadata:

creation_time : 2018-06-06T01:11:10.000000Z

handler_name : ISO Media file produced by Google Inc.

Created on: 06/05/2018.

vendor_id : [0][0][0][0]

Stream mapping:

Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))

Stream #0:1 -> #0:1 (aac (native) -> aac (native))

Press [q] to stop, [?] for help

[libx264 @ 000002996b8a90c0] using SAR=9/8

[libx264 @ 000002996b8a90c0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2

[libx264 @ 000002996b8a90c0] profile High, level 1.3, 4:2:0, 8-bit

[libx264 @ 000002996b8a90c0] 264 - core 164 - H.264/MPEG-4 AVC codec - Copyleft 2003-2022 - http://www.videolan.org/x264.html - options:
cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=7 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00

Output #0, mp4, to 'filename1.mp4':

Metadata:

major_brand : mp42

minor_version : 0

compatible_brands: isommp42

encoder : Lavf59.17.102

Stream #0:0(und): Video: h264 (avc1 / 0x31637661), yuv420p(tv, smpte170m/smpte170m/bt709, progressive), 320x240 [SAR 9:8 DAR 3:2], q=2-31, 29.97 fps, 30k tbn (default)

Metadata:

creation_time : 2018-06-06T01:11:10.000000Z

handler_name : ISO Media file produced by Google Inc.
Created on: 06/05/2018.

vendor_id : [0][0][0][0]

encoder : Lavc59.21.100 libx264

Side data:

cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)

Metadata:

creation_time : 2018-06-06T01:11:10.000000Z

handler_name : ISO Media file produced by Google Inc.
Created on: 06/05/2018.

vendor_id : [0][0][0][0]

encoder : Lavc59.21.100 aac

frame=76693 fps=454 q=-1.0 Lsize= 115454kB time=00:42:39.04 bitrate= 369.6kbits/s speed=15.2x

video:73329kB audio:39501kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 2.325049%

[libx264 @ 000002996b8a90c0] frame I:462 Avg QP:20.30 size: 13259

[libx264 @ 000002996b8a90c0] frame P:30634 Avg QP:23.42 size: 1629

[libx264 @ 000002996b8a90c0] frame B:45597 Avg QP:28.92 size: 418

[libx264 @ 000002996b8a90c0] consecutive B-frames: 17.7% 7.0% 6.5% 68.8%

[libx264 @ 000002996b8a90c0] mb I I16..4: 6.8% 50.1% 43.0%

[libx264 @ 000002996b8a90c0] mb P I16..4: 1.1% 3.5% 0.9% P16..4: 27.7% 13.2% 8.8% 0.0% 0.0% skip:44.8%

[libx264 @ 000002996b8a90c0] mb B I16..4: 0.1% 0.4% 0.2% B16..8: 31.0% 5.4% 1.3% direct: 1.5% skip:60.2% L0:39.0% L1:50.2% BI:10.9%

[libx264 @ 000002996b8a90c0] 8x8 transform intra:60.1% inter:58.6%

[libx264 @ 000002996b8a90c0] coded y,uvDC,uvAC intra: 62.2% 55.5% 24.7% inter: 12.3% 9.1% 0.8%

[libx264 @ 000002996b8a90c0] i16 v,h,dc,p: 22% 38% 9% 31%

[libx264 @ 000002996b8a90c0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 17% 24% 20% 5% 6% 6% 9% 5% 8%

[libx264 @ 000002996b8a90c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 18% 24% 13% 6% 8% 7% 10% 6% 8%

[libx264 @ 000002996b8a90c0] i8c dc,h,v,p: 59% 21% 15% 5%

[libx264 @ 000002996b8a90c0] Weighted P-Frames: Y:7.2% UV:3.0%

[libx264 @ 000002996b8a90c0] ref P L0: 71.3% 14.3% 10.4% 3.7% 0.3%

[libx264 @ 000002996b8a90c0] ref B L0: 92.5% 6.1% 1.5%

[libx264 @ 000002996b8a90c0] ref B L1: 96.2% 3.8%

[libx264 @ 000002996b8a90c0] kb/s:234.74

[aac @ 000002996c0dcdc0] Qavg: 3985.249
这是 python 设置唯一消息,使用:
subprocess.call(['ffmpeg', '-i', input_file, '-filter:v', 'scale=.{}/{}*iw:ih'.format(x, y), output_file])

[libx264 @ 0000029d2d338e00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2

[libx264 @ 0000029d2d338e00] profile High, level 3.0, 4:2:0, 8-bit

[libx264 @ 0000029d2d338e00] 264 - core 164 - H.264/MPEG-4 AVC codec - Copyleft 2003-2022 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=11 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00

Output #0, mp4, to 'C:/filename1.mp4':

Metadata:

major_brand : mp42

minor_version : 0

compatible_brands: isommp42

encoder : Lavf59.17.102

Stream #0:0(und): Video: h264 (avc1 / 0x31637661), yuv420p(tv, smpte170m/smpte170m/bt709, progressive), 540x360 [SAR 1:1 DAR 3:2], q=2-31, 29.97 fps, 30k tbn (default)

Metadata:

creation_time : 2018-06-06T01:11:10.000000Z

handler_name : ISO Media file produced by Google Inc. Created on: 06/05/2018.

vendor_id : [0][0][0][0]

encoder : Lavc59.21.100 libx264

Side data:

cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A

Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)

Metadata:

creation_time : 2018-06-06T01:11:10.000000Z

handler_name : ISO Media file produced by Google Inc. Created on: 06/05/2018.

vendor_id : [0][0][0][0]

encoder : Lavc59.21.100 aac

最佳答案

如果您需要达到一定程度的压缩(或文件大小),您需要指定您的目标。如果您使用 H.264 视频编解码器输出 MP4,请阅读 this FFmpeg Wiki entry .你也可以试试H.265 这可以使您的文件大小再减少 25–50%(同样取决于您的编码配置),但会占用更多的计算能力(或时间)。
[22 年 3 月 16 日添加]
我原来的答案是不正确的。您得到 2 个不同结果(cli vs. Python)的原因是过滤器不一样:
命令行:scale=320:240 .非常直接,输出将是 320 像素宽 x 240 像素高
Python 表达式:'scale=.{x}/{y}*iw:ih' .虽然 x 的值和 y未公开,高度保持不变,而宽度通过一些有趣的数学修改(x 表示分子的小数部分除以 y 作为分母,乘以输入宽度)。如果您的意图是输出与 x=320 的 cli 案例相同尺寸的视频和 y=240那么表达式应该简单地读作:scale={x}:{y} .

关于ffmpeg - Python ffmpeg 在缩放文件大小方面的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71483608/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com