Python如何实现视频裁剪(python,开发技术)

时间:2024-05-03 05:33:03 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

环境依赖

本文主要使用到的不是ffmpeg,而是ffprobe也在上面这篇文章中的zip包中。

Python如何实现视频裁剪

ffmpy安装:

pipinstallffmpy-ihttps://pypi.douban.com/simple

代码

不废话,上代码。

#!/user/bin/envpython#coding=utf-8"""@project:csdn@author:剑客阿良_ALiang@file:cut_out_pic_tool.py@ide:PyCharm@time:2022-01-2010:38:53"""importosimportuuidfromffmpyimportFFmpeg#视频裁剪defcut_out_video(video_path:str,output_dir:str,start_pix:tuple,size:tuple):ext=os.path.basename(video_path).strip().split('.')[-1]ifextnotin['mp4','avi','flv']:raiseException('formaterror')result=os.path.join(output_dir,'{}.{}'.format(uuid.uuid1().hex,ext))ff=FFmpeg(inputs={video_path:None},outputs={result:'-vfcrop={}:{}:{}:{}-y-threads5-presetultrafast-strict-2'.format(size[0],size[1],start_pix[0],start_pix[1])})print(ff.cmd)ff.run()returnresultif__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、截取的视频宽高不能超过原视频大小。

验证一下

准备的视频分辨率。

Python如何实现视频裁剪

执行结果

C:\Users\huyi\.conda\envs\python36\python.exe"C:\ProgramFiles\JetBrains\PyCharm2020.1.3\plugins\python\helpers\pydev\pydevconsole.py"--mode=client--port=3021importsys;print('Python%son%s'%(sys.version,sys.platform))sys.path.extend(['D:\\spyder\\csdn','D:/spyder/csdn'])PyDevconsole:starting.Python3.6.13|Anaconda,Inc.|(default,Mar162021,11:37:27)[MSCv.191664bit(AMD64)]onwin32runfile('D:/spyder/csdn/cut_out_video_tool.py',wdir='D:/spyder/csdn')ffmpeg-iC:\Users\huyi\Desktop\test2.mp4-vfcrop=512:512:0:0-y-threads5-presetultrafast-strict-2C:\Users\huyi\Desktop\afc1de8879f211eca313e454e8bf1461.mp4ffmpegversionn4.3.1-20-g8a2acdc6daCopyright(c)2000-2020theFFmpegdevelopersbuiltwithgcc9.3-win32(GCC)20200320configuration:--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=-lgomplibavutil56.51.100/56.51.100libavcodec58.91.100/58.91.100libavformat58.45.100/58.45.100libavdevice58.10.100/58.10.100libavfilter7.85.100/7.85.100libswscale5.7.100/5.7.100libswresample3.7.100/3.7.100libpostproc55.7.100/55.7.100Input#0,mov,mp4,m4a,3gp,3g2,mj2,from'C:\Users\huyi\Desktop\test2.mp4':Metadata:major_brand:isomminor_version:512compatible_brands:isomiso2avc1mp41encoder:Lavf58.45.100description:PackedbyBilibiliXCoderv2.0.2Duration:00:09:00.58,start:0.000000,bitrate:1558kb/sStream#0:0(und):Video:h364(High)(avc1/0x31637661),yuv420p,1920x1080[SAR1:1DAR16:9],1556kb/s,23.98fps,23.98tbr,24ktbn,47.95tbc(default)Metadata:handler_name:VideoHandlerStreammapping:Stream#0:0->#0:0(h364(native)->h364(libx264))Press[q]tostop,[?]forhelp[libx264@000002069d5e7200]usingSAR=1/1[libx264@000002069d5e7200]usingcpucapabilities:MMX2SSE2FastSSSE3SSE4.2AVXFMA3BMI2AVX2[libx264@000002069d5e7200]profileConstrainedBaseline,level3.0,4:2:0,8-bit[libx264@000002069d5e7200]264-core161-H.264/MPEG-4AVCcodec-Copyleft2003-2020-http://www.videolan.org/x264.html-options:cabac=0ref=1deblock=0:0:0analyse=0:0me=diasubme=0psy=1psy_rd=1.00:0.00mixed_ref=0me_range=16chroma_me=1trellis=08x8dct=0cqm=0deadzone=21,11fast_pskip=1chroma_qp_offset=0threads=5lookahead_threads=1sliced_threads=0nr=0decimate=1interlaced=0bluray_compat=0constrained_intra=0bframes=0weightp=0keyint=250keyint_min=23scenecut=0intra_refresh=0rc=crfmbtree=0crf=23.0qcomp=0.60qpmin=0qpmax=69qpstep=4ip_ratio=1.40aq=0Output#0,mp4,to'C:\Users\huyi\Desktop\afc1de8879f211eca313e454e8bf1461.mp4':Metadata:major_brand:isomminor_version:512compatible_brands:isomiso2avc1mp41description:PackedbyBilibiliXCoderv2.0.2encoder:Lavf58.45.100Stream#0:0(und):Video:h364(libx264)(avc1/0x31637661),yuv420p,512x512[SAR1:1DAR1:1],q=-1--1,23.98fps,24ktbn,23.98tbc(default)Metadata:handler_name:VideoHandlerencoder:Lavc58.91.100libx264Sidedata:cpb:bitratemax/min/avg:0/0/0buffersize:0vbv_delay:N/Aframe=12961fps=945q=-1.0Lsize=30056kBtime=00:09:00.54bitrate=455.5kbits/sspeed=39.4xvideo:30004kBaudio:0kBsubtitle:0kBotherstreams:0kBglobalheaders:0kBmuxingoverhead:0.173805%[libx264@000002069d5e7200]frameI:52AvgQP:12.98size:25387[libx264@000002069d5e7200]frameP:12909AvgQP:15.91size:2278[libx264@000002069d5e7200]mbII16..4:100.0%0.0%0.0%[libx264@000002069d5e7200]mbPI16..4:7.0%0.0%0.0%P16..4:27.6%0.0%0.0%0.0%0.0%skip:65.4%[libx264@000002069d5e7200]codedy,uvDC,uvACintra:25.4%24.6%3.3%inter:10.3%7.3%0.2%[libx264@000002069d5e7200]i16v,h,dc,p:46%20%15%19%[libx264@000002069d5e7200]i8cdc,h,v,p:61%16%19%4%[libx264@000002069d5e7200]kb/s:454.67C:\Users\huyi\Desktop\afc1de8879f211eca313e454e8bf1461.mp4

结果视频分辨率

Python如何实现视频裁剪

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Python如何实现视频裁剪的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:如何分析Linux内存、Swap、Cache和Buffer下一篇:

10 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18