Ffmpeg py版本的简单视频处理

import subprocess
import uuid
import os


class FfmpegUtils:
    def __init__(self, ffmpeg_path='ffmpeg', media_output_path='D:\\audio\\'):
        self.ffmpeg_path = ffmpeg_path
        self.media_output_path = media_output_path

    # 提取视频中的音频
    def video_extract_audio(self, input_video, volume):
        media_output_path = self.media_output_path
        random_filename = str(uuid.uuid4()).replace("-", "")[:12]
        output_audio_path = media_output_path + '\\' + random_filename + ".m4a"
        command = ["-i", input_video, "-vn", "-af", f"volume={volume}", "-y", output_audio_path]
        full_command = [self.ffmpeg_path] + command
        process = subprocess.Popen(full_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output, error = process.communicate()
        if process.returncode != 0:
            print(f"Error executing command: {' '.join(full_command)}")
            print(error.decode('utf-8'))
            exit()
        else:
            return output_audio_path

    # 提取视频中的画面
    def video_extract_frame(self, input_video):
        media_output_path = self.media_output_path
        random_filename = str(uuid.uuid4()).replace("-", "")[:12]
        output_audio_path = media_output_path + '\\' + random_filename + ".mp4"
        command = ["-i", input_video, "-an", "-vcodec", "copy", output_audio_path]
        full_command = [self.ffmpeg_path] + command
        process = subprocess.Popen(full_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output, error = process.communicate()
        if process.returncode != 0:
            print(f"Error executing command: {' '.join(full_command)}")
            print(error.decode('utf-8'))
            exit()
        else:
            return output_audio_path

    # 合并两个音频文件 时长以第一文件为准
    def audio_merge(self, input_audio1, input_audio2):
        media_output_path = self.media_output_path
        random_filename = str(uuid.uuid4()).replace("-", "")[:12]
        output_audio_path = media_output_path + '\\' + random_filename + ".mp3"
        command = ["-i", input_audio1, "-i", input_audio2, "-filter_complex",
                   "amix=inputs=2:duration=first:dropout_transition=2", "-f", "mp3", output_audio_path]
        full_command = [self.ffmpeg_path] + command
        process = subprocess.Popen(full_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output, error = process.communicate()
        if process.returncode != 0:
            print(f"Error executing command: {' '.join(full_command)}")
            print(error.decode('utf-8'))
            exit()
        else:
            return output_audio_path

    # 合成视频与音频文件
    def merge_audio_video(self, input_video, input_audio):
        media_output_path = self.media_output_path
        random_filename = str(uuid.uuid4()).replace("-", "")[:12]
        output_path = os.path.join(media_output_path, random_filename + ".mp4")
        command = ["-i", input_video, "-i", input_audio, "-c:v", "copy", "-map", "0:v:0", "-map", "1:a:0", "-c:a",
                   "aac", output_path]
        full_command = [self.ffmpeg_path] + command
        process = subprocess.Popen(full_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output, error = process.communicate()
        if process.returncode != 0:
            print(f"Error executing command: {' '.join(full_command)}")
            print(error.decode('utf-8'))
            exit()
        else:
            return output_path


# 使用示例
if __name__ == "__main__":
    test = FfmpegUtils()
    input_video1 = "D:\\audio\\1.mp4"
    # input_audio1 = "D:\\audio\\1.mp3"
    # test.merge_audio_video(input_video1, input_audio1)
    output,error = os.remove(input_video1)

有工作需求 随手记录一下demo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值