目录
前言
本文提供将视频调整分辨率的Python代码,一如既往的实用主义。
环境依赖
ffmpeg环境安装,可以参考我的另一篇文章:windows ffmpeg安装部署_阿良的博客-CSDN博客
ffmpy安装:
pip install ffmpy -i https://pypi.douban.com/simple
代码
不废话上代码。
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/12/12 14:07 # @Author : 剑客阿良_ALiang # @Site : # @File : video_tool.py import os import uuid from ffmpy import FFmpeg # 调整视频大小 def change_size(video_path: str, output_dir: str, width: int, height: int, bit_rate=2000): ext = os.path.basename(video_path).strip().split('.')[-1] if ext not in ['mp4']: raise Exception('format error') _result_path = os.path.join( output_dir, '{}.{}'.format( uuid.uuid1().hex, ext)) ff = FFmpeg(inputs={'{}'.format(video_path): None}, outputs={ _result_path: '-s {}*{} -b {}k'.format(width, height, bit_rate)}) print(ff.cmd) ff.run() return _result_path
代码说明:
1、change_size方法入参分别为:视频地址、输出目录、宽度、高度,码率(默认为2000k,已经算很高了)。
2、格式校验只有mp4,可以自行添加。
3、文件名用uuid,避免重复。
验证一下:
准备的视频如下:
验证代码:
if __name__ == '__main__': print(change_size('C:/Users/yi/Desktop/981.mp4', 'C:/Users/yi/Desktop', 960, 540))
执行结果:
E:\ProgramData\Anaconda3\envs\pytorch\python.exe C:/Users/yi/PycharmProjects/test/video_tool.py ffmpeg -i C:/Users/yi/Desktop/981.mp4 -s 960*540 -b 2000k C:/Users/yi/Desktop\8b854c985b1411eca9982c4d54eea02b.mp4 ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers built with gcc 9.3-win32 (GCC) 20200320 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 --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --disable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp libavutil 56. 51.100 / 56. 51.100 libavcodec 58. 91.100 / 58. 91.100 libavformat 58. 45.100 / 58. 45.100 libavdevice 58. 10.100 / 58. 10.100 libavfilter 7. 85.100 / 7. 85.100 libswscale 5. 7.100 / 5. 7.100 libswresample 3. 7.100 / 3. 7.100 libpostproc 55. 7.100 / 55. 7.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:/Users/yi/Desktop/981.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 title : ���ɱ�ֲ�̫��(���ݼ�����).Leon.1994.Directors.Cut.720p.Bluray encoder : Lavf58.45.100 copyright : bbs.cnxp.com(C)2009 description : This File is Created by Easy RealMedia Tools@! keywords : Ӱ�ӵ۹���Ʒ Duration: 00:00:05.64, start: 0.000000, bitrate: 1017 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x688, 881 kb/s, 23.98 fps, 23.98 tbr, 785647.00 tbn, 47.95 tbc (default) Metadata: handler_name : VideoHandler Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default) Metadata: handler_name : SoundHandler Please use -b:a or -b:v, -b is ambiguous 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 @ 000001757623e580] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2 [libx264 @ 000001757623e580] profile High, level 3.1, 4:2:0, 8-bit [libx264 @ 000001757623e580] 264 - core 161 - H.264/MPEG-4 AVC codec - Copyleft 2003-2020 - 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=12 lookahead_threads=2 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=23 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=abr mbtree=1 bitrate=2000 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00 Output #0, mp4, to 'C:/Users/yi/Desktop\8b854c985b1411eca9982c4d54eea02b.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 title : ���ɱ�ֲ�̫��(���ݼ�����).Leon.1994.Directors.Cut.720p.Bluray keywords : Ӱ�ӵ۹���Ʒ copyright : bbs.cnxp.com(C)2009 description : This File is Created by Easy RealMedia Tools@! encoder : Lavf58.45.100 Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 960x540, q=-1--1, 2000 kb/s, 23.98 fps, 785647.00 tbn, 23.98 tbc (default) Metadata: handler_name : VideoHandler encoder : Lavc58.91.100 libx264 Side data: cpb: bitrate max/min/avg: 0/0/2000000 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: handler_name : SoundHandler encoder : Lavc58.91.100 aac frame= 122 fps= 98 q=-1.0 Lsize= 1173kB time=00:00:05.01 bitrate=1914.6kbits/s speed=4.02x video:1087kB audio:79kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.552477% [libx264 @ 000001757623e580] frame I:1 Avg QP:16.01 size: 50847 [libx264 @ 000001757623e580] frame P:31 Avg QP:12.50 size: 24730 [libx264 @ 000001757623e580] frame B:90 Avg QP:17.24 size: 3280 [libx264 @ 000001757623e580] consecutive B-frames: 1.6% 0.0% 0.0% 98.4% [libx264 @ 000001757623e580] mb I I16..4: 17.6% 53.9% 28.5% [libx264 @ 000001757623e580] mb P I16..4: 1.3% 3.8% 1.2% P16..4: 23.6% 21.4% 18.5% 0.0% 0.0% skip:30.2% [libx264 @ 000001757623e580] mb B I16..4: 0.1% 0.2% 0.1% B16..8: 27.3% 8.4% 3.2% direct: 3.6% skip:57.2% L0:38.3% L1:46.7% BI:14.9% [libx264 @ 000001757623e580] final ratefactor: 13.48 [libx264 @ 000001757623e580] 8x8 transform intra:58.6% inter:57.4% [libx264 @ 000001757623e580] coded y,uvDC,uvAC intra: 65.5% 76.2% 67.7% inter: 18.4% 14.7% 4.1% [libx264 @ 000001757623e580] i16 v,h,dc,p: 53% 16% 13% 18% [libx264 @ 000001757623e580] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 36% 15% 21% 3% 5% 6% 4% 6% 4% [libx264 @ 000001757623e580] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 35% 18% 13% 5% 7% 8% 5% 6% 4% [libx264 @ 000001757623e580] i8c dc,h,v,p: 48% 22% 24% 7% [libx264 @ 000001757623e580] Weighted P-Frames: Y:0.0% UV:0.0% [libx264 @ 000001757623e580] ref P L0: 63.2% 13.5% 17.3% 6.0% [libx264 @ 000001757623e580] ref B L0: 89.6% 8.8% 1.7% [libx264 @ 000001757623e580] ref B L1: 95.1% 4.9% [libx264 @ 000001757623e580] kb/s:1749.39 [aac @ 00000175743fe940] Qavg: 180.360 C:/Users/yi/Desktop\8b854c985b1411eca9982c4d54eea02b.mp4
执行效果:
OK,分辨率调整为960×540。
总结
没什么好总结的。
分享:
What is real? How do you define 'real'? If you're talking about what you can feel, what you can smell, what you can taste and see, then 'real' is simply electrical signals interpreted by your brain. ——《黑客帝国》
如果本文对你有帮助的话,给个赞吧,谢谢!
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试