python - 当我在 Python 中使用命令时,命令的运行与预期不同

标签 python python-3.x powershell cmd ffmpeg

我有一个代码,我将 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.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)
    
  • 使用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.exec:\FFmpeg\bin文件夹。
    您可以复制ffmpeg.exec:\FFmpeg\bin , 或使用 ffmpeg.exe 的实际文件夹.
  • 您也可以添加包含 ffmpeg.exe 的文件夹到如下guide中描述的系统路径.
  • 关于python - 当我在 Python 中使用命令时,命令的运行与预期不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69217092/

    相关文章:

    python - 什么是 "python -m SimpleHTTPServer"的 Python 3 等价物

    MySQL 密码在 Windows 中被 powershell 修改

    python - 选择具有多处理的进程数时是否有任何准则可遵循?

    python - 将 PNG 图像编码为 Base64 时出现 AttributeError

    python - 如何在 pandas 数据框中堆叠 wthin 来执行其引用?

    powershell - powershell函数返回用户定义类的实例将导致构造函数返回另一个新实例

    powershell - 如果触发了if语句,然后在触发If -And语句之前退出if语句

    Python 异常处理 - 如何做 pythonic?

    python - 迭代多个Excel文件,使用python将特定单元格保存到数据框中

    python - RuntimeError : Given groups=1, 大小 [64, 3, 3, 3] 的权重,预期输入 [4, 5000, 5000, 3] 有 3 个 channel ,但有 5000 个 channel