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