我正在努力写一个gui FFMPEG.我正在使用pythons子进程为我想要的每个转换创建一个ffmpeg进程.这工作正常,但我也想要一种方法来获得转换的进度,无论是否失败等等.我想我可以通过访问进程的标准输出这样做:
调用 subprocess.Popen()
# Convert - Calls FFMPEG with current settings. (in a seperate
# thread.)
def convert(self):
# Check if options are valid
if self.input == "" or self.output == "":
return False
# Make the command string
ffmpegString = self.makeString()
# Try to open with these settings
try:
self.ffmpeg = subprocess.Popen(ffmpegString, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except OSError:
self.error.append("OSError: ")
except ValueError:
self.error.append("ValueError: Couldn't call FFMPEG with these parameters")
# Convert process should be running now.
Run Code Online (Sandbox Code Playgroud)
阅读stdout:
convert = Convert()
convert.input = "test.ogv"
convert.output = "test.mp4"
convert.output_size = (0, 0)
convert.convert()
while 1:
print convert.ffmpeg.stdout.readline()
Run Code Online (Sandbox Code Playgroud)
这有效,但ffmpeg的状态不显示.我假设它与ffmpeg刷新它的方式有关.有没有办法访问它?
小智 7
只需添加,universal_newlines = True到您的subprocess.Popen行.
cmd="ffmpeg -i in.mp4 -y out.avi"
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines=True)
for line in process.stdout:
print(line)
Run Code Online (Sandbox Code Playgroud)
现在你的周期就像:
frame= 1900 fps=453 q=18.6 Lsize= 3473kB time=00:01:16.08 bitrate= 373.9kbits/s
Run Code Online (Sandbox Code Playgroud)
使用time =值确定百分比进度.