【问题标题】:how can duration from all mp3 files [duplicate]所有mp3文件的持续时间如何[重复]
【发布时间】:2020-04-12 07:56:32
【问题描述】:

我想统计一个目录中的所有 mp3 文件。

import os
from subprocess import check_output

directory = r'c:\test'

sum = 0

for filename in os.listdir(directory):
    if filename.endswith(".mp3"):
        a = str(check_output('ffprobe -i  "'+filename +'" 2>&1 |findstr "Duration"',shell=True))
        a = a.split(",")[0].split("Duration:")[1].strip()
        h, m, s = a.split(':')
        duration = int(h) * 3600 + int(m) * 60 + float(s)
        print(os.path.join(directory, filename, duration))
        sum = sum + duration

sum = round(sum)
print("sum", sum)
time = datetime.timedelta(seconds=sum)
print(str(time))

我收到此错误,但我不知道为什么。有人可以帮我解决错误吗?非常感谢

File "loop.py", line 10
a = str(check_output('ffprobe -i  "'+filename+'" 2>&1 |findstr "Duration"',shell=True))
                                                                                       ^
TabError: inconsistent use of tabs and spaces in indentation

【问题讨论】:

  • file_name 变量在哪里?而且我认为您运行a = str(check_output('ffprobe -i "'+file_name+'" 2>&1 |findstr "Duration"',shell=True)) 的地方没有适当的缩进。重新格式化以正常工作
  • 我更正了文件名,但错误仍然存​​在
  • 查看我注意到的第二个问题,即缩进问题。因此,将 for loop 下面的所有代码用 back Tab 移动两次。按一个将缩进到 if 循环并位于 if loop 下方的向前选项卡,然后按一次向前选项卡。所以缩进会被保留。
  • 您的编辑器混合了制表符和空格以进行缩进。 python 3 不接受这表达了对这个错误的悲伤。使用可以将选项卡显示为特殊字符并修复它们的编辑器。

标签: python-3.x


【解决方案1】:

这个错误是任何程序员的祸根。根据您的 IDE,您可以使用 online tool 或全选,缩进然后取消缩进,但是,现在,您只需复制下面的代码,理论上应该可以修复错误(您可能会注意到它看起来一样,但相信我):

import os
from subprocess import check_output

directory = r'c:\test'

sum = 0

for filename in os.listdir(directory):
    if filename.endswith(".mp3"):
        a = str(check_output('ffprobe -i  "'+file_name+'" 2>&1 |findstr "Duration"',shell=True))
        a = a.split(",")[0].split("Duration:")[1].strip()
        h, m, s = a.split(':')
        duration = int(h) * 3600 + int(m) * 60 + float(s)
        print(os.path.join(directory, filename, duration))
        sum = sum + duration

sum = round(sum)
print("sum", sum)
time = datetime.timedelta(seconds=sum)
print(str(time))

编辑:
新错误的根源在于 duration 不是字符串。使用str()进行转换:

import os
from subprocess import check_output

directory = r'c:\test'

sum = 0

for filename in os.listdir(directory):
    if filename.endswith(".mp3"):
        a = str(check_output('ffprobe -i  "'+file_name+'" 2>&1 |findstr "Duration"',shell=True))
        a = a.split(",")[0].split("Duration:")[1].strip()
        h, m, s = a.split(':')
        duration = int(h) * 3600 + int(m) * 60 + float(s)
        print(os.path.join(directory, filename, str(duration)))
        sum = sum + duration

sum = round(sum)
print("sum", sum)
time = datetime.timedelta(seconds=sum)
print(str(time))

【讨论】:

  • 很好,它可以工作,但我现在有另一个错误: Traceback(最近一次调用最后一次):文件“loop3.py”,第 14 行,在 print(os.path.join(目录、文件名、持续时间))文件“C:\Users\xxx\AppData\Local\Programs\Python\Python38-32\lib\ntpath.py”,第 117 行,在 join genericpath._check_arg_types('join', path, *paths) 文件 "C:\Users\xxx\AppData\Local\Programs\Python\Python38-32\lib\genericpath.py",第 152 行,在 _check_arg_types 中引发 TypeError(f'{funcname}() 参数必须是 str , bytes, or ' TypeError: join() argument must be str, bytes, or os.PathLike object, not 'float'
  • @kuck 我已经更新了原始版本以包含对您最新错误的修复。
  • 非常感谢,现在可以正常工作了。
猜你喜欢
  • 1970-01-01
  • 2019-01-27
  • 2013-05-12
  • 2018-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多