我一直试图在视频的音频中找到音频静音空间的位置,但我无法通过在python 3中使用pydub导入音频文件
我已经尝试将pydub检查ffmpeg的目录更改为项目中的一个,并且该文件位于我正在运行脚本的目录中,但它似乎仍然返回相同的错误。
from moviepy import editor
from pydub import silence, AudioSegment
from pathlib import Path
import os
AudioSegment.converter = r"C:\\Users\\ratee\\PycharmProjects\\untitled\\ffmpeg\\bin\\ffmpeg.exe"
vid = editor.VideoFileClip("video.mp4")
print(AudioSegment.ffmpeg)
my_file = Path("audio.mp3")
if not my_file.is_file():
vid.audio.write_audiofile("audio.mp3")
audio = AudioSegment.from_mp3("audio.mp3")
print(audio)
我希望它将mp3音频片段存储到变量audi
中,但它返回:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1741, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1735, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1135, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/ratee/PycharmProjects/untitled/fuc.py", line 12, in <module>
song = AudioSegment.from_mp3("audio.mp3")
File "C:\Users\ratee\PycharmProjects\untitled\venv\lib\site-packages\pydub\audio_segment.py", line 716, in from_mp3
return cls.from_file(file, 'mp3', parameters=parameters)
File "C:\Users\ratee\PycharmProjects\untitled\venv\lib\site-packages\pydub\audio_segment.py", line 665, in from_file
info = mediainfo_json(orig_file)
File "C:\Users\ratee\PycharmProjects\untitled\venv\lib\site-packages\pydub\utils.py", line 263, in mediainfo_json
res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
File "C:\Users\ratee\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 756, in __init__
restore_signals, start_new_session)
File "C:\Users\ratee\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1155, in _execute_child
startupinfo)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\_pydev_bundle\pydev_monkey.py", line 452, in new_CreateProcess
return getattr(_subprocess, original_name)(app_name, patch_arg_str_win(cmd_line), *args)
FileNotFoundError: [WinError 2] The system cannot find the file specified
print(AudioSegment.ffmpeg)
按预期返回
C:\ Users \用户受评者\ PycharmProjects \无\ ffmpeg的\ BIN \ ffmpeg.exe
print(my_file) returns
按预期返回
audio.mp3
并且代码在我尝试导入音频时停止运行
audio = AudioSegment.from_mp3(“audio.mp3”)
这不是一个aswer,而只是为了比较原因尝试重现OPs错误。 Foldernames x,y用于缩短路径长度。
c:\x\lib\site-packages\pydub\utils.py:165: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
c:\x\lib\site-packages\pydub\utils.py:193: RuntimeWarning: Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work
warn("Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work", RuntimeWarning)
Traceback (most recent call last):
File "C:\y\lol.py", line 16, in <module>
audi = AudioSegment.from_mp3("audio.mp3")
File "c:\x\lib\site-packages\pydub\audio_segment.py", line 716, in from_mp3
return cls.from_file(file, 'mp3', parameters=parameters)
File "c:\x\lib\site-packages\pydub\audio_segment.py", line 665, in from_file
info = mediainfo_json(orig_file)
File "c:\x\lib\site-packages\pydub\utils.py", line 263, in mediainfo_json
res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
File "c:\x\lib\subprocess.py", line 676, in __init__
restore_signals, start_new_session)
File "c:\x\lib\subprocess.py", line 957, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
以下代码:
AudioSegment.ffmpeg = os.getcwd()+"\\ffmpeg\\bin\\ffmpeg.exe"
print (AudioSegment.ffmpeg)
Ffgfeeph.exe。\ T.
和:
my_file = Path("audio.mp3")
print (my_file)
得到:
2)audio.mp3
建议的解决方案:
包括以下特定代码行以指定ffmpeg所在的位置:
pydub.AudioSegment.converter = r"C:\\path\\to\\ffmpeg.exe"
其中path \ to \是实际路径
而你应该没事。
更新1:
引发错误的原因是使用了“my_file”中的文件名,而不是AudioSegment.from_mp3(my_file)
所要求的“filepath”。通过提供文件路径,这将修复引发的[WinError2]问题。
当您在脚本下运行时,会出现AttributeError: 'WindowsPath' object has no attribute 'read'
错误。该错误与pathlib
有关,应该在pydub 0.22版本中修复,如github上讨论的here。我在Github举起了issue。
引发的file.read()
问题与python版本相关(2.7对3.5),因为它在内置库中不再存在。因此,.read()
触发AttributeError: 'WindowsPath' object has no attribute 'read'
错误。
from pydub import silence, AudioSegment
from pathlib import Path
import os, sys
print (sys.version)
#AudioSegment.ffmpeg = os.getcwd()+"\\ffmpeg\\bin\\ffmpeg.exe"
AudioSegment.converter = r"C:\\x\\build\\win\\64\\ffmpeg.exe"
AudioSegment.ffprobe = r"C:\\x\\build\\win\\64\\ffprobe.exe"
#print (AudioSegment.converter)
#print (AudioSegment.ffprobe)
my_file = Path("C:\\y\\audio.mp3")
print ('ID1 : %s' % my_file)
audio = AudioSegment.from_mp3(my_file) # solves ***[WinError2]*** issue.