基于Python实现视频分辨率转换
更新时间:2021年12月13日 08:58:15 作者:剑客阿良_ALiang
这篇文章主要介绍了基于Python实现视频的分辨率转换的示例代码,文中的代码讲解详细,对学习Python有一定的帮助,感兴趣的小伙伴可以了解一下
前言
本文提供将视频调整分辨率的Python代码,一如既往的实用主义。
环境依赖
ffmpeg环境安装,可以参考文章:windows ffmpeg安装部署
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,分辨率调整为960x540。
到此这篇关于基于Python实现视频分辨率转换的文章就介绍到这了,更多相关Python 视频分辨率转换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
python playwright--pytest-playwright、pytest-base-url插件编
这篇文章主要介绍了python playwright--pytest-playwright、pytest-base-url插件编写用例,它提供上下文隔离,开箱即用地在多个浏览器配置上运行,它继承了pytest框架,以及支持playwright的一些基础使用,需要的朋友可以参考下2023-05-05
最新评论