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

2025-11-07 0 154

代码示例

# 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 = rffprobe -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 = rffmpeg -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 = rffmpeg -y -i “{}” -i “{}” -filter_complex “[0:a] \ rpan=stereo|c0=1*c0|c1=1*c1 [a1], [1:a] \ rpan=stereo|c0=1*c0|c1=1*c1 [a2],[a1][a2]amix=duration=first, \ rpan=stereo|c0

以上就是Python调用ffmpeg开源视频处理库,批量处理视频的详细内容。

您可能感兴趣的文章: 如何在 Python 中使用 tryelse 块 关于Python的异常捕获和处理 关于使用Python的time库制作进度条程序 关于python的第三方库下载与更改方式 Python的turtle绘图库使用基础 python如何用正则表达式提取字符串 python中的txt文件转换为XML 详解Python中四种关系图数据可视化的效果对比

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

遇见资源网 后端开发 Python调用ffmpeg开源视频处理库,批量处理视频 https://www.ox520.com/4495.html

常见问题

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务