【发布时间】:2016-02-19 19:40:18
【问题描述】:
我正在使用不同的程序 (ffmpeg) 来获取已下载的 youtube 视频的长度,以便随机化视频中的特定点。但是,当我尝试执行此代码时出现此错误:
def grabTimeOfDownloadedYoutubeVideo(youtubeVideo):
process = subprocess.Popen(['/usr/local/bin/ffmpeg', '-i', youtubeVideo], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = process.communicate()
matches = str(re.search(b"Duration:\s{1}(?P<hours>\d+?):(?P<minutes>\d+?):(?P<seconds>\d+\.\d+?),", stdout, re.DOTALL).groupdict()).encode()
print(matches)
hours = int(matches['hours'])
minutes = int(matches['minutes'])
seconds = int(matches['seconds'])
total = 0
total += 60 * 60 * hours
total += 60 * minutes
total += seconds
print(total)
matches 变量打印出来:
b"{'minutes': b'04', 'hours': b'00', 'seconds': b'24.94'}"
所以所有的输出都以'b'开头。如何删除“b”并获取数字?
这里有完整的错误信息:
Traceback (most recent call last):
File "bot.py", line 87, in <module>
grabTimeOfDownloadedYoutubeVideo("videos/1.mp4")
File "bot.py", line 77, in grabTimeOfDownloadedYoutubeVideo
hours = int(matches['hours'])
TypeError: byte indices must be integers or slices, not str
【问题讨论】:
-
stackoverflow.com/a/6269785/5575315 也许你正在寻找的东西