编写一个小压缩程序,但我添加的使其成为黑白的选项也会删除声音,我不完全确定这是我的代码有问题还是过滤器没有保留声音文件。我只是想知道让它工作的唯一方法是在最后重新组合视频的音频文件,这可能会使输出文件超过压缩目标。帮助表示赞赏。
import os, ffmpeg
from tkinter import filedialog
def compressVideo(videoFullPath, videoFileName, targetSize):
minAudioBitrate = 32000
maxAudioBitrate = 256000
probe = ffmpeg.probe(videoFullPath)
duration = float(probe['format']['duration'])
audioBitrate = float(next((s for s in probe['streams'] if s['codec_type'] == 'audio'), None)['bit_rate'])
targetTotalBitrate = (targetSize * 1024 * 8) / (1.073741824 * duration)
if 10 * audioBitrate > targetTotalBitrate:
audioBitrate = targetTotalBitrate / 10
if audioBitrate < minAudioBitrate < targetTotalBitrate:
audioBitrate = minAudioBitrate
elif audioBitrate > maxAudioBitrate:
audioBitrate = maxAudioBitrate
videoBitrate = targetTotalBitrate - audioBitrate
i = ffmpeg.input(videoFullPath)
ffmpeg.output(i,'NUL',
**{'c:v': 'libx264', 'b:v': videoBitrate, 'pass': 1, 'f': 'null'}
).overwrite_output().run()
##PROBLEM THIS BIT HERE, SOUND NOT KEPT
i = ffmpeg.filter_(i,'hue', s='0')
ffmpeg.output(i, videoFileName,
**{'c:v': 'libx264', 'b:v': videoBitrate, 'pass': 2, 'c:a': 'aac', 'b:a': audioBitrate, 'f' :'mp4', }
).overwrite_output().run()
def fileSelect(): #start of fileSelect function
global startingLocation #declares startingLocation as global variable
global originalName #declares originalName as global variable
global fileType #declares fileType as global variable
startingLocation = filedialog.askopenfilename(initialdir="/", title="Select file", #tkinter function that opens file explorer, lets user select file saves the file path as a variable
filetypes=[("MP4 FILES", "*.mp4")])
originalName = os.path.basename(startingLocation)
fileSelect()
compressVideo(startingLocation, 'customName'+'.mp4', 8 * 1000)