Python调用ffmpeg开源视频处理库,批量处理视频


Posted in Python onNovember 16, 2020

代码示例

# coding=utf-8
import os
import subprocess
import datetime
import json, pprint
import re, time
import threading
import random
import shutil


class FFmpeg:

  def __init__(self, editvdo, addlogo=None, addmusic=None,
         addvdohead=None, addvdotail=None):
    self.editvdo = editvdo
    self.addlogo = addlogo
    self.addmusic = addmusic
    self.addvdohead = addvdohead
    self.addvdotail = addvdotail
    self.vdo_time, self.vdo_width, self.vdo_height, self.attr_dict = self.get_attr()
    self.editvdo_path = os.path.dirname(editvdo)
    self.editvdo_name = os.path.basename(editvdo)

  def get_attr(self):
    """
    获取视频属性参数
    :return:
    """
    strcmd = r'ffprobe -print_format json -show_streams -i "{}"'.format(self.editvdo)
    status, output = subprocess.getstatusoutput(strcmd)
    agrs = eval(re.search('{.*}', output, re.S).group().replace("\n", "").replace(" ", ''))
    streams = agrs.get('streams', [])
    agrs_dict = dict()
    [agrs_dict.update(x) for x in streams]
    vdo_time = agrs_dict.get('duration')
    vdo_width = agrs_dict.get('width')
    vdo_height = agrs_dict.get('height')
    attr = (vdo_time, vdo_width, vdo_height, agrs_dict)
    return attr

  def edit_head(self, start_time, end_time, deposit=None):
    """
    截取指定长度视频
    :param second: 去除开始的多少秒
    :param deposit: 另存为文件
    :return: True/Flase
    """
    if None == deposit:
      deposit = self.editvdo_path+'/'+'edit_head'+self.editvdo_name
    start = time.strftime('%H:%M:%S', time.gmtime(start_time))
    end = time.strftime('%H:%M:%S', time.gmtime(end_time))
    strcmd = 'ffmpeg -i "{}" -vcodec copy -acodec copy -ss {} -to {} "{}" -y'.format(
      self.editvdo, start, end, deposit)
    result = subprocess.run(args=strcmd, stdout=subprocess.PIPE, shell=True)
    if os.path.exists(deposit):
      os.remove(self.editvdo)
      os.rename(deposit, self.editvdo)
      return True
    else:
      return False

  def edit_logo(self, deposit=None):
    """
    添加水印
    :param deposit:添加水印后另存为路径,为空则覆盖
    :return: True/False
    """
    if None == deposit:
      deposit = self.editvdo_path+'/'+'edit_logo'+self.editvdo_name
    strcmd = r'ffmpeg -i "{}" -vf "movie=\'{}\' [watermark];[in] ' \
         r'[watermark] overlay=main_w-overlay_w-10:10 [out]" "{}"'.format(
          self.editvdo, self.addlogo, deposit)
    result = subprocess.run(args=strcmd, stdout=subprocess.PIPE, shell=True)
    if os.path.exists(deposit):
      os.remove(self.editvdo)
      os.rename(deposit, self.editvdo)
      return True
    else:
      return False

  def edit_music(self, deposit=None):
    if None == deposit:
      deposit = self.editvdo_path+'/'+'edit_music'+self.editvdo_name
    strcmd = r'ffmpeg -y -i "{}" -i "{}" -filter_complex "[0:a] ' \
         r'pan=stereo|c0=1*c0|c1=1*c1 [a1], [1:a] ' \
         r'pan=stereo|c0=1*c0|c1=1*c1 [a2],[a1][a2]amix=duration=first,' \
         r'pan=stereo|c0<c0+c1|c1<c2+c3,pan=mono|c0=c0+c1[a]" ' \
         r'-map "[a]" -map 0:v -c:v libx264 -c:a aac ' \
         r'-strict -2 -ac 2 "{}"'.format(self.editvdo, self.addmusic, deposit)
    result = subprocess.run(args=strcmd, stdout=subprocess.PIPE, shell=True)
    if os.path.exists(deposit):
      os.remove(self.editvdo)
      os.rename(deposit, self.editvdo)
      return True
    else:
      return False

  def edit_rate(self, rete=30, deposit=None):
    """
    改变帧率
    :param rete: 修改大小帧率
    :param deposit: 修改后保存路径
    :return:
    """
    if None == deposit:
      deposit = self.editvdo_path+'/'+'edit_music'+self.editvdo_name
    strcmd = r'ffmpeg -i "{}" -r {} "{}"' % (self.editvdo, rete, deposit)
    result = subprocess.run(args=strcmd, stdout=subprocess.PIPE, shell=True)
    if os.path.exists(deposit):
      os.remove(self.editvdo)
      os.rename(deposit, self.editvdo)
      return True
    else:
      return False

  def edit_power(self, power='1280x720', deposit=None):
    """
    修改分辨率
    :param power: 分辨率
    :param deposit: 修改后保存路径,为空则覆盖
    :return:
    """
    if None == deposit:
      deposit = self.editvdo_path+'/'+'edit_power'+self.editvdo_name
    strcmd = r'ffmpeg -i "{}" -s {} "{}"'.format(self.editvdo, power, deposit)
    result = subprocess.run(args=strcmd, stdout=subprocess.PIPE, shell=True)
    if os.path.exists(deposit):
      os.remove(self.editvdo)
      os.rename(deposit, self.editvdo)
      return True
    else:
      return False

  def rdit_marge(self, vdo_head, vdo_tail, deposit=None):
    if None == deposit:
      deposit = self.editvdo_path+'/'+'rdit_marge'+self.editvdo_name
    with open(self.editvdo_path+'/'+'rdit_marge.txt', 'w', encoding='utf-8') as f:
      f.write("file '{}' \nfile '{}' \nfile '{}'" .format(
        vdo_head, self.editvdo, vdo_tail))
    strcmd = r'ffmpeg -f concat -safe 0 -i "{}" -c copy "{}"'.format(
      self.editvdo_path + '/' + 'rdit_marge.txt', deposit)
    result = subprocess.run(args=strcmd, stdout=subprocess.PIPE, shell=True)
    if os.path.exists(deposit):
      os.remove(self.editvdo)
      os.rename(deposit, self.editvdo)
      return True
    else:
      return False



  # ffmpeg - i input.mkv - filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" - map"[v]" - map"[a]" output.mkv



test = FFmpeg(r"D:\vdio\4.mp4")
pass

以上就是Python调用ffmpeg开源视频处理库,批量处理视频的详细内容,更多关于python 批量处理视频的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python使用mysqldb连接数据库操作方法示例详解
Dec 03 Python
python在Windows8下获取本机ip地址的方法
Mar 14 Python
Python实现TCP/IP协议下的端口转发及重定向示例
Jun 14 Python
用python做一个搜索引擎(Pylucene)的实例代码
Jul 05 Python
基于Python __dict__与dir()的区别详解
Oct 30 Python
Python扩展内置类型详解
Mar 26 Python
python3 实现验证码图片切割的方法
Dec 07 Python
Python实现求两个数组交集的方法示例
Feb 23 Python
Python Multiprocessing多进程 使用tqdm显示进度条的实现
Aug 13 Python
python爬虫beautifulsoup解析html方法
Dec 07 Python
关于python爬虫应用urllib库作用分析
Sep 04 Python
python基础之//、/与%的区别详解
Jun 10 Python
python tkinter实现连连看游戏
Nov 16 #Python
详解python os.path.exists判断文件或文件夹是否存在
Nov 16 #Python
Python 删除List元素的三种方法remove、pop、del
Nov 16 #Python
python 从list中随机取值的方法
Nov 16 #Python
python实现在列表中查找某个元素的下标示例
Nov 16 #Python
python如何获得list或numpy数组中最大元素对应的索引
Nov 16 #Python
Python实现列表索引批量删除的5种方法
Nov 16 #Python
You might like
浅谈PHP语法(1)
2006/10/09 PHP
谈PHP生成静态页面分析 模板+缓存+写文件
2009/08/17 PHP
php保存任意网络图片到服务器的方法
2015/04/14 PHP
PHP加密技术的简单实现
2016/09/04 PHP
PHP使用preg_split()分割特殊字符(元字符等)的方法分析
2017/02/04 PHP
动态调用CSS文件的JS代码
2010/07/29 Javascript
JS 添加千分位与去掉千分位的示例
2013/07/11 Javascript
使用js写的一个简易的投票
2013/11/27 Javascript
js/jquery判断浏览器的方法小结
2014/09/02 Javascript
js添加千分位的实现代码(超简单)
2016/08/01 Javascript
jQuery正则验证注册页面经典实例
2017/06/10 jQuery
JavaScript30 一个月纯 JS 挑战中文指南(英文全集)
2017/07/23 Javascript
vue绑定设置属性的多种方式(5)
2017/08/16 Javascript
jQuery插件Validation表单验证详解
2018/05/26 jQuery
JS实现中英文混合文字溢出友好截取功能
2018/08/06 Javascript
详解如何修改 node_modules 里的文件
2020/05/22 Javascript
python实现基于信息增益的决策树归纳
2018/12/18 Python
python3对接mysql数据库实例详解
2019/04/30 Python
python基于pdfminer库提取pdf文字代码实例
2019/08/15 Python
python数据化运营的重要意义
2019/11/25 Python
在ipython notebook中使用argparse方式
2020/04/20 Python
PyQT5 实现快捷键复制表格数据的方法示例
2020/06/19 Python
两种CSS3伪类选择器详细介绍
2013/12/24 HTML / CSS
Myprotein意大利官网:欧洲第一运动营养品牌
2018/11/22 全球购物
以实惠的价格轻松租车,免费取消:Easyrentcars
2019/07/16 全球购物
曼城官方网上商店:Manchester City
2019/09/10 全球购物
凌阳科技股份有限公司C++程序员面试题笔试题
2014/11/20 面试题
Ajax实现页面无刷新留言效果
2021/03/24 Javascript
技校生自我鉴定
2013/12/08 职场文书
劳资人员岗位职责
2013/12/19 职场文书
养殖人员的创业计划书范文
2013/12/26 职场文书
促销活动策划方案
2014/01/12 职场文书
和睦家庭事迹
2014/05/14 职场文书
《我爱祖国》演讲稿1000字
2014/09/26 职场文书
挂靠协议书
2015/01/27 职场文书
十二月早安励志心语大全
2019/12/03 职场文书