【问题标题】:Command is running different from expected when i use it trought Python当我在 Python 中使用命令时,命令的运行与预期不同
【发布时间】:2021-11-11 22:59:37
【问题描述】:

我有一个代码,我将 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

【问题讨论】:

  • 注意subprocess.run(["ffmpeg","-i","C:\YTDownloads\CurrentAudio.3gpp","C:\YTDownloads\CurrentAudio.mp3]")中最后一个引号位置不对,它必须出现在之前右括号...

标签: python python-3.x powershell cmd ffmpeg


【解决方案1】:

所有命令都应该可以工作(有一些修复)...

您可以尝试以下方法:

  1. 添加-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)
    
  2. 确保ffmpeg.exe 在系统路径中。
    如果您不确定ffmpeg.exe 是否在系统路径中,请尝试使用完整路径。
    例如假设ffmpeg.exec:\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)
    
  3. 使用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 所述。

【讨论】:

  • 谢谢!有效!我想我的路径环境有问题,因为 ffmpeg 实际上在那里,但我现在会使用这种方式,谢谢,真的,你帮了很多忙
猜你喜欢
  • 1970-01-01
  • 2016-10-13
  • 2018-12-28
  • 2020-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
相关资源
最近更新 更多