我有一个代码,我将 youtube 视频下载为 3gpp 并将其转换为 mp3,我需要使用 FFmpeg 来执行此操作,并且在使用 cmd 和 powershell 时效果很好,但是,当我尝试运行相同的命令时Python,它根本不起作用。
这是我的命令:ffmpeg -i C:\YTDownloads\CurrentAudio.3gpp C:\YTDownloads\CurrentAudio.mp3
我试过:subprocess.call(r'%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe ffmpeg -i C:\YTDownloads\CurrentAudio.3gpp C:\YTDownloads\CurrentAudio.mp3', shell=True)
subprocess.run(["ffmpeg","-i","C:\YTDownloads\CurrentAudio.3gpp","C:\YTDownloads\CurrentAudio.mp3]")
os.system('powershell ffmpeg -i C:\YTDownloads\CurrentAudio.3gpp C:\YTDownloads\CurrentAudio.mp3')
subprocess.run([
'ffmpeg',
'-i', os.path.join(parent_dir, f"{newname}.3gpp"),
os.path.join(parent_dir, f"{newname}.mp3")
])
subprocess.call('C:\Windows\System32\powershell.exe ffmpeg -i C:\YTDownloads\CurrentAudio.3gpp C:\YTDownloads\CurrentAudio.mp3', shell=True)
它们都返回某种类型的错误,其中一些返回 ffmpeg 不是被识别为内部命令,在另一些中它说系统找不到指定的路径,但它们都不起作用,甚至认为当我在 cmd/powershell 上使用完全相同的命令时,它可以完美运行。对不起我的英语不好:3
最佳答案
所有命令都应该工作(有一些修复)......
您可以尝试以下方法:
-y
不询问就覆盖输出文件的参数。从 Python 执行时,您可能看不到以下问题:
File 'C:\YTDownloads\CurrentAudio.mp3' already exists. Overwrite ? [y/N]
添加示例 -y
:subprocess.call(r'%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe ffmpeg -y -i C:\YTDownloads\CurrentAudio.3gpp C:\YTDownloads\CurrentAudio.mp3', shell=True)
ffmpeg.exe
在系统路径中。如果您不确定
ffmpeg.exe
在系统路径中,尝试使用完整路径。例如假设
ffmpeg.exe
在 c:\FFmpeg\bin\
,使用完整路径:subprocess.call(r'%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe c:\FFmpeg\bin\ffmpeg -y -i C:\YTDownloads\CurrentAudio.3gpp C:\YTDownloads\CurrentAudio.mp3', shell=True)
r""
前缀,或使用双反斜杠:替换
os.system('powershell c:\FFmpeg\bin\ffmpeg -y -i C:\YTDownloads\CurrentAudio.3gpp C:\YTDownloads\CurrentAudio.mp3')
和:os.system(r'powershell c:\FFmpeg\bin\ffmpeg -y -i C:\YTDownloads\CurrentAudio.3gpp C:\YTDownloads\CurrentAudio.mp3')
#or: os.system('powershell c:\\FFmpeg\\bin\\ffmpeg -y -i C:\\YTDownloads\\CurrentAudio.3gpp C:\\YTDownloads\\CurrentAudio.mp3')
替换
subprocess.run(['ffmpeg', '-i', os.path.join(parent_dir, f"{newname}.3gpp"), os.path.join(parent_dir, f"{newname}.mp3")])
和:parent_dir = r'C:\YTDownloads'
newname = 'CurrentAudio'
subprocess.run([
r'c:\FFmpeg\bin\ffmpeg', '-y',
'-i', os.path.join(parent_dir, f"{newname}.3gpp"),
os.path.join(parent_dir, f"{newname}.mp3")
])
替换
subprocess.call('C:\Windows\System32\powershell.exe ffmpeg -i C:\YTDownloads\CurrentAudio.3gpp C:\YTDownloads\CurrentAudio.mp3', shell=True)
和:subprocess.call(r'c:\FFmpeg\bin\ffmpeg -y -i C:\YTDownloads\CurrentAudio.3gpp C:\YTDownloads\CurrentAudio.mp3')
无需使用powershell
并且无需使用shell=True
.笔记:
ffmpeg.exe
在 c:\FFmpeg\bin
文件夹。您可以复制
ffmpeg.exe
至c:\FFmpeg\bin
, 或使用 ffmpeg.exe
的实际文件夹. ffmpeg.exe
的文件夹到如下guide中描述的系统路径. 关于python - 当我在 Python 中使用命令时,命令的运行与预期不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69217092/