目录
- 前言
- 环境依赖
- 代码
- 验证一下
前言
本文提供将视频按照自定义尺寸进行裁剪的工具方法,一如既往的实用主义。
环境依赖
ffmpeg环境安装,可以参考文章:windows ffmpeg安装部署
本文主要使用到的不是ffmpeg,而是ffprobe也在上面这篇文章中的zip包中。
ffmpy安装:
pip install ffmpy -i https://pypi.douban.com/simple
代码
不废话,上代码。
#!/user/bin/env python # coding=utf-8 """ @project : csdn @author : 剑客阿良_ALiang @file : cut_out_pic_tool.py @ide : PyCharm @time : 2022-01-20 10:38:53 """ import os import uuid from ffmpy import FFmpeg # 视频裁剪 def cut_out_video(video_path: str, output_dir: str, start_pix: tuple, size: tuple): ext = os.path.basename(video_path).strip().split('.')[-1] if ext not in ['mp4', 'avi', 'flv']: raise Excepti编程客栈on('format error') result = os.path.join(output_dir, '{}.{}'.format(uuid.uuid1().hex, ext)) ff = FFmpeg(inputs={video_path: None}, outputs={ result: '-vf crop={}:{}:{}:{} -y -threads 5 -preset ultrafast -strict -2'.format(size[0], size[1], start_pix[0], start_pix[1])}) print(ff.cmd) ff.run() return result if __name__ == '__main__': print(cut_out_video(r'C:\Users\huyi\Desktop\test2.mp4', r'C:\Users\huyi\Desktop', (0, 0), (512, 512)))
代码说明
1、cut_out_video方法参数分别为,视频地址、输出目录地址、起始像素点位置、需要裁剪的宽高。
2、做了简单的视频格式校验,如需添加,可以自己看着来。
3、最终视频名使用uuid避免重复。
4、截取的视频宽高不能超过原视频大小。
验证一下
准备的视频分辨率。
执行结果
C:\Users\huyi\.conda\envs\python36\python.exe "C:\Program Files\JetBrains\PyCharm 2020.1.3\plugins\python\helpers\pydev\pydevconsole.py" --mode=client --port=3021 import sys; print('Python %s on %s' % (sys.version, sys.platform)) sys.path.extend(['D:\\spyder\\csdn', 'D:/spyder/csdn']) PyDev console: starting. Python 3.6.13 |Anaconda, Inc.| (default, Mar 16 2021, 11:37:27) [MSC v.1916 64 bit (AMD64)] on win32 runfile('D:/spyder/csdn/cut_out_video_tool.py', wdir='D:/spyder/csdn') ffmpeg -i C:\Users\huyi\Desktop\test2.mp4 -vf crop=512:512:0:0 -y -threads 5 -preset ultrafast -strict -2 C:\Users\huyi\Desktop\afc1de8879f211eca313e454e8bf1461.mp4 ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers built with gcc 9.3-win32 (GCC) 20200320 configur编程客栈ation: --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 KMbEFzqe--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\huyi\Desktop\test2.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf58.45.100 description : Packed by Bilibili XCoder v2.0.2 Duration: 00:09:00.58, start: 0.000000, bitrate: 1558 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 1556 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc (default) Metadata: handler_name : VideoHandler Stream mapping: Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264)) Press [q] to stop, [?] for help [libx264 @ 000002069d5e7200] using SAR=1/1 [libx264 @ 000002069d5e7200] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2 [libx264 @ 000002069d5e7200] profile Constrained Baseline, level 3.0, 4:2:0, 8-bit [libx264 @ 000002069d5e7200] 264 - core 161 - H.264/MPEG-4 AVC codec - Copyleft 2003-2020 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=5 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=23 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0 Output #0, mp4, to 'C:\Users\huyi\Desktop\afc1de8879f211eca313e454e8bf1461.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 description : Packed by Bilibili XCoder v2.0.2 encoder : Lavf58.45.100 Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yu编程客栈v420p, 512x512 [SAR 1:1 DAR 1:1], q=-1--1, 23.98 fps, 24k tbn, 23.98 tbc (default) Metadata: handler_name : VideoHandler encoder : Lavc58.91.100 libx264 Side data: cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A frame=12961 fps=945 q=-1.0 Lsize= 30056kB time=00:09:00.54 bitrate= 455.5kbits/s speed=39.4x video:30004kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.173805% [libx264 @ 000002069d5e7200] frame I:52 Avg QP:12.98 size: 25387 [libx264 @ 000002069d5e7200] frame P:12909 Avg QP:15.91 size: 2278 [libx264 @ 000002069d5e7200] mb I I16..4: 100.0% 0.0% 0.0% [libx264 @ 000002069d5e7200] mb P I16..4: 7.0% 0.0% 0.0% P16..4: 27.6% 0.0% 0.0% 0.0% 0.0% skip:65.4% [libx264 @ 000002069d5e7200] coded y,uvDC,uvAC intra: 25.4% 24.6% 3.3% inter: 10.3% 7.3% 0.2% [libx264 @ 000002069d5e7200] i16 v,h,dc,p: 46% 20% 15% 19% [libx264 @ 000002069d5e7200] i8c dc,h,v,p: 61% 16% 19% 4% [libx264 @ 000002069d5e7200] kb/s:454.67 C:\Users\huyi\Desktop\afc1de8879f211eca313e454http://www.cppcns.come8bf1461.mp4
结果视频分辨率
以上就是Python实现视频裁剪的示例代码的详细内容,更多关于Python视频裁剪的资料请关注我们其它相关文章!
精彩评论