欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于Python实现视频分辨率转换

程序员文章站 2022-03-13 15:54:47
目录前言环境依赖代码验证一下前言本文提供将视频调整分辨率的python代码,一如既往的实用主义。环境依赖ffmpeg环境安装,可以参考文章:ffmpy安装:pip install ffmpy -i h...

前言

本文提供将视频调整分辨率的python代码,一如既往的实用主义。

环境依赖

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
 
 
# 调整视频大小
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

代码说明:

1、change_size方法入参分别为:视频地址、输出目录、宽度、高度,码率(默认为2000k,已经算很高了)。

2、格式校验只有mp4,可以自行添加。

3、文件名用uuid,避免重复。

验证一下

准备的视频如下:

基于Python实现视频分辨率转换

验证代码:

if __name__ == '__main__':
    print(change_size('c:/users/yi/desktop/981.mp4', 'c:/users/yi/desktop', 960, 540))

执行结果:

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

执行效果:

基于Python实现视频分辨率转换

ok,分辨率调整为960x540。

到此这篇关于基于python实现视频分辨率转换的文章就介绍到这了,更多相关python 视频分辨率转换内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!