的情况:,我正在使用ffmpeg (通过.net)保存视频文件。我可以从ffmpeg获得输出,但我不知道如何定制输出以获得更好的结果。
我的问题:我的问题是,成功的操作和失败的操作没有一定的区别。
最后一行成功: 视频:1006 0kB音频:134 0kB字幕:0全局标头:0 0kB 最后一行从失败 c:\x\test-9-8/30/2012-9:29:56-AM.mp4:无效参数 rtmp://cdn.tv/cdn-live39/definst/微流01:发生未知错误
我的问题是:是否有一个选项(或命令行参数)来添加某种返回代码(200:成功,500:错误等等)
谢谢!
PS:我看到了这个话题How to tell if ffmpeg errored?,但是在最后一行之前/之后没有数字。我想上一个版本已经没有号码了。
发布于 2015-06-12 11:06:42
我知道这是非常古老的,但当我发现没有其他可靠的答案,并经过更多的测试后:
检查返回值为0的建议通常是一个很好的建议--但并不是在所有情况下都有帮助。检查文件是否存在的另一个想法也是好的--但同样,在所有情况下都不起作用。
例如,当输入文件是具有嵌入式封面的mp3文件时,ffmpeg就会(在我的测试中)使用此图像并将其提取为(不可用的)视频文件。
我现在要做的是让调试级输出并解析它,以获得被屏蔽的数据包的数量。
ffmpeg -i "wildlife.mp4" -c:v copy -an -sn "out.mp4" -y -loglevel debug 2> wildlife.txt
我使用正则表达式搜索以下文本:
Output stream .+ (video): [0-9][0-9]+ packets muxed \([0-9][0-9]+ bytes\)
(这假设每个视频都有9个以上的数据包-当然可以为非常短的视频进行优化)。
当然,对于RTMP或其他设置,输出可能有所不同,但我认为解析完整的输出流是唯一的选择。
发布于 2012-08-31 07:05:34
您只需检查ffmpeg返回的退出代码即可。它应该在成功时返回0,其他任何东西都意味着它失败了。
发布于 2022-10-04 12:26:58
您可以在-v error模式下运行ffmpeg,并让它将错误返回到文本文件中,请参见此处:https://superuser.com/questions/100288/how-can-i-check-the-integrity-of-a-video-file-avi-mpeg-mp4 --您可以将它与没有null输出的编码结合起来,但是您只能从文本文件中读取结果。
或者您可以有一个额外的脚本来跟踪错误。下面是一个检查文件完整性的Python示例,注意if stdout子句。这将基本上重新检查编码文件,如果你需要看到正常的输出第一。
解决方案1:
import subprocess
import os
import sys
import re
def check_video(files):
if type(files) == str:
files = [files]
for file in files:
print(f"Checking {file}...")
command_line = f'ffmpeg -v error -i "{file}" -map 0:v -map 0:a? -vcodec copy -acodec copy -f null -'
base_name = os.path.splitext(file)[0]
extension = os.path.splitext(file)[1]
process = subprocess.Popen(command_line, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = process.communicate()
return_code = process.returncode
pid = process.pid
print(f"Base: {base_name}")
print(f"Extension: {extension}")
print(f"RC: {return_code}")
if stdout:
allowed_errs = ["invalid as first byte of an EBML number"]
stdout_detect = stdout.decode().split("\n")
for error in allowed_errs:
if error not in stdout_detect[0] or len(stdout_detect) > 2:
print(f"Errors!")
print(os.path.splitext(file)[1])
os.rename(file, f"{file}.error")
with open(f"{file}.error.log", "w") as errfile:
if stdout:
errfile.write(stdout.decode())
if stderr:
errfile.write(stderr.decode())
else:
print("Minor problems detected.")
else:
print("File OK.")
process.wait()
if __name__ == "__main__":
files = sys.argv[1:]
# files = ["a.mkv"]
check_video(files)解决方案2:
with subprocess.Popen(command_line,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True) as self.process:
for line in self.process.stdout:
print(line, end='')
return_code = self.process.wait()从这里开始,您可以对每个line做您想做的任何事情,比如检查它们中的错误关键字。我认为ffmpeg有一些错误报告标准(error.html)。使用此解决方案,输出将显示给您,就像直接运行ffmpeg一样。资料来源:https://stackoverflow.com/a/4417735/1793606.用Python 3.10进行测试
解决方案3:,您也可以为ffmpeg本身设置err_detect,这应该反映在返回代码中。
https://stackoverflow.com/questions/12199216
复制相似问题