Python实现视频裁剪的示例代码
程序员文章站
2022-06-23 13:51:50
目录前言环境依赖代码验证一下前言本文提供将视频按照自定义尺寸进行裁剪的工具方法,一如既往的实用主义。环境依赖ffmpeg环境安装,可以参考文章:本文主要使用到的不是ffmpeg,而是ffprobe也在...
前言
本文提供将视频按照自定义尺寸进行裁剪的工具方法,一如既往的实用主义。
环境依赖
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 exception('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 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\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), yuv420p, 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\afc1de8879f211eca313e454e8bf1461.mp4
结果视频分辨率
以上就是python实现视频裁剪的示例代码的详细内容,更多关于python视频裁剪的资料请关注其它相关文章!
上一篇: 鬼哭狼嚎的雷人孩子