基于Python實(shí)現(xiàn)視頻分辨率轉(zhuǎn)換
前言
本文提供將視頻調(diào)整分辨率的Python代碼,一如既往的實(shí)用主義。
環(huán)境依賴
ffmpeg環(huán)境安裝,可以參考文章: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
# 調(diào)整視頻大小
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
代碼說(shuō)明:
1、change_size方法入?yún)⒎謩e為:視頻地址、輸出目錄、寬度、高度,碼率(默認(rèn)為2000k,已經(jīng)算很高了)。
2、格式校驗(yàn)只有mp4,可以自行添加。
3、文件名用uuid,避免重復(fù)。
驗(yàn)證一下
準(zhǔn)備的視頻如下:

驗(yàn)證代碼:
if __name__ == '__main__':
print(change_size('C:/Users/yi/Desktop/981.mp4', 'C:/Users/yi/Desktop', 960, 540))
執(zhí)行結(jié)果:
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
執(zhí)行效果:

OK,分辨率調(diào)整為960x540。
到此這篇關(guān)于基于Python實(shí)現(xiàn)視頻分辨率轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)Python 視頻分辨率轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
9行Python3代碼實(shí)現(xiàn)批量提取PDF文件的指定內(nèi)容
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)9行Python3代碼實(shí)現(xiàn)批量提取PDF文件的指定內(nèi)容,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-12-12
Python實(shí)戰(zhàn)項(xiàng)目之MySQL tkinter pyinstaller實(shí)現(xiàn)學(xué)生管理系統(tǒng)
讀萬(wàn)卷書不如行萬(wàn)里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用MySQL、tkinter、 pyinstaller實(shí)現(xiàn)一個(gè)學(xué)生管理系統(tǒng),大家可以通過(guò)案例查缺補(bǔ)漏,提升水平2021-10-10
Python機(jī)器學(xué)習(xí)之Kmeans基礎(chǔ)算法
這篇文章主要介紹了Python機(jī)器學(xué)習(xí)之Kmeans基礎(chǔ)算法,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
win10下python3.5.2和tensorflow安裝環(huán)境搭建教程
這篇文章主要為大家詳細(xì)介紹了win10下python3.5.2和tensorflow安裝環(huán)境搭建教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
詳解Pytorch如何利用yaml定義卷積網(wǎng)絡(luò)
大多數(shù)卷積神經(jīng)網(wǎng)絡(luò)都是直接通過(guò)寫一個(gè)Model類來(lái)定義的,這樣寫的代碼其實(shí)是比較好懂,也很方便。但是本文將介紹另一個(gè)方法:利用yaml定義卷積網(wǎng)絡(luò),感興趣的可以了解一下2022-10-10
利用Python實(shí)現(xiàn)多種風(fēng)格的照片處理
這篇文章主要為大家詳細(xì)介紹了如何利用Python一鍵實(shí)現(xiàn)多種風(fēng)格的照片處理并制作可視化GUI界面,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-07-07
Python中的HTTP請(qǐng)求庫(kù)Requests的具體使用
Python作為一種功能強(qiáng)大且易于學(xué)習(xí)的編程語(yǔ)言,提供了許多用于處理HTTP請(qǐng)求的庫(kù),其中,Requests庫(kù)是最受歡迎的選擇之一,本文主要介紹了Python中的HTTP請(qǐng)求庫(kù)Requests的具體使用,感興趣的可以了解一下2023-12-12
python?基本結(jié)構(gòu)語(yǔ)句(函數(shù)和模塊)
這篇文章主要介紹了python?基本結(jié)構(gòu)語(yǔ)句(函數(shù)和模塊),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
python實(shí)現(xiàn)超市進(jìn)銷存管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)超市進(jìn)銷存管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06

