我有一个音频文件,Sample.flac
。可以使用 ffprobe 读取标题和长度,以将输出发送到 STDERR。
我想通过子进程运行ffprobe
,并且已经成功完成。然后,我使用 *.communicate()[1].decode()
检索输出(通过管道传输到 subprocess.PIPE
),正如 Python 文档所指示的那样。
communicate()
返回一个元组 (stdout, stderr)
,其中包含 Popen()
对象的输出。然后,访问 stderr
的正确索引并将其从字节字符串解码为 Python 3 UTF-8 字符串。
然后使用与 ffprobe
元数据输出的格式匹配的多行正则表达式模式来解析此解码输出。然后将匹配组适本地放入字典中,将每个第一组转换为小写,并用作第二组的键(值)。
Here is an example of the output and the working regex.
可以按预期通过字典键访问数据。但将这些值连接在一起(全部都是字符串)时,输出会出现损坏。
这是我期望的输出:
Believer (Kaskade Remix) 190
相反,这就是我得到的:
190ever (Kaskade Remix)
我不明白为什么字符串看起来彼此“重叠”并导致损坏的形式。谁能解释一下这一点以及我做错了什么?
下面是运行以产生上述结果的完整代码。这是我整个项目的缩减部分。
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import os
from re import findall, MULTILINE
from subprocess import Popen, PIPE
def media_metadata(file_path):
"""Use FFPROBE to get information about a media file."""
stderr = Popen(("ffprobe", file_path), shell=True, stderr=PIPE).communicate()[1].decode()
metadata = {}
for match in findall(r"(\w+)\s+:\s(.+)$", stderr, MULTILINE):
metadata[match[0].lower()] = match[1]
return metadata
if __name__ == "__main__":
meta = media_metadata("C:/Users/spike/Music/Sample.flac")
print(meta["title"], meta["length"])
# The above and below have the same result in the console
# print(meta["title"] + " " + meta["length"])
# print("{title} {length}".format(meta))
谁能解释一下这种不可预测的输出?
我问过这个问题here早些时候,但我认为这不是很清楚。在 raw output当它在多个文件上运行时,您可以看到,到最后,字符串开始变得不可预测,甚至根本不打印部分 title
值。
谢谢。
最佳答案
您正在追赶“\r”符号。打印时,光标返回到字符串的开头,因此下一次打印并覆盖第一部分。删除空格(还将删除尾随的“\r”)应该可以解决问题:
metadata[match[0].lower()] = match[1].strip()
关于python - 连接正则表达式从子进程 STDERR 检索到的字符串会导致困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48676862/