In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to achieve video clipping Python, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
Environmental dependence
Instead of using ffmpeg for this article, ffprobe is also included in the zip package in the above article.
Ffmpy installation:
Pip install ffmpy-I https://pypi.douban.com/simple code
No nonsense, go to the code.
#! / user/bin/env python# coding=utf-8 "" @ project: csdn@author: swordsman A Liang _ ALiang@file: cut_out_pic_tool.py@ide: PyCharm@time: 2022-01-20 10:38:53 "import osimport uuidfrom ffmpy import FFmpeg # Video clipping 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 (ritual C:\ Users\ huyi\ Desktop\ test2.mp4', ritual C:\ Users\ huyi\ Desktop' (0,0), (512512))
Code description
1. The parameters of cut_out_video method are video address, output directory address, starting pixel location, width and height to be cropped.
2. A simple video format check has been done. If you need to add it, you can watch it yourself.
3. Use uuid to avoid repetition of the final video name.
4. The width and height of the captured video cannot exceed the size of the original video.
Verify it.
Prepared video resolution.
Execution result
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=3021import sys Print ('Python% s on% s'% s'(sys.version, sys.platform)) sys.path.extend (['D:\\ spyder\\ csdn', 'DVR]) PyDev console: starting.Python 3.6.13 | Anaconda, Inc. | (default, Mar 16 2021, 11:37:27) [MSC v.1916 64 bit (AMD64)] on win32runfile (' DDDRG SPYDERA SPYDNGUP cutout outside videoengine 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.mp4ffmpeg 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-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-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.100Input # 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: 0015 09purl 00.58, start: 0.000000, bitrate: 1558 kb/s Stream # 0:0 (und): Video: h364 (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: VideoHandlerStream mapping: Stream # 0:0-> # 0:0 (h364 (native)-> h364 (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=0Output # 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: h364 (libx264) (avc1 / 0x31637661), yuv420p, 512x512 [SAR 1:1 DAR 1:1] 23.98 tbc (default) Metadata: handler_name: VideoHandler encoder: Lavc58.91.100 libx264 Side data: cpb: bitrate max/min/avg: 0 vbv_delay 0 buffer size: 0 vbv_delay: N/Aframe=12961 fps=945 qintlue 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 libx264 52 Avg QP:12.98 size: 25387 [libx264 @ 000002069d5e7200] frame Pva12909 Avg QP:15.91 size: 2278 [libx264 @ 000002069d5e7200] mb I 16. 4: 100.0% 0.00% [libx264 @ 000002069d5e7200] mb P I16: 7.0% 0.00% 0.00% P16.. 4: 27.6% 0.06 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 vJournal dcPowerp: 46% 20% 15% 19% [libx264 @ 000002069d5e7200] i8c dc,h,v,p: 61% 16% 19% [libx264 @ 000002069d5e7200] kb/s:454.67C:\ Users\ huyi\ Desktop\ afc1de8879f211eca313e454e8bf1461.mp4
Results Video resolution
The above is all the content of this article "how to achieve video clipping in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.