【问题标题】:Converting mkv files to mp4 with ffmpeg-python使用 ffmpeg-python 将 mkv 文件转换为 mp4
【发布时间】:2021-02-07 16:31:20
【问题描述】:

我有很多 .mkv 文件正在尝试转换为 .mp4,因此我决定尝试在 python 中编写解决方案。几个小时后,我试图弄清楚如何复制子文件夹,我放弃了,决定坚持转换单个子文件夹,然后将它们复制到另一个目录。

我制作了一个简单的脚本,它应该转换与脚本位于同一文件夹中的 .mkv 文件。但是,我不断收到此错误:

FileNotFoundError: [WinError 2] 系统找不到指定的文件

这是我的代码:

import os
import ffmpeg

start_dir = os.getcwd()

def convert_to_mp4(mkv_file):
    no_extension = str(os.path.splitext(mkv_file))
    with_mp4 = no_extension + ".mp4"
    ffmpeg.input(mkv_file).output(with_mp4).run()
    print("Finished converting {}".format(no_extension))

for path, folder, files in os.walk(start_dir):
    for file in files:
        if file.endswith('.mkv'):
            print("Found file: %s" % file)
            convert_to_mp4(file)
        else:
            pass

【问题讨论】:

    标签: python-3.x ffmpeg converters file-conversion


    【解决方案1】:

    嗯,答案总是比你想象的要简单。

    归结为:

    def convert_to_mp4(mkv_file):
        name, ext = os.path.splitext(mkv_file)
        out_name = name + ".mp4"
        ffmpeg.input(mkv_file).output(out_name).run()
        print("Finished converting {}".format(mkv_file))
    
    for path, folder, files in os.walk(start_dir):
        for file in files:
            if file.endswith('.mkv'):
                print("Found file: %s" % file)
                convert_to_mp4(os.path.join(start_dir, file))
            else:
                pass
    

    确保 ffmpeg.exe 与脚本位于同一目录中。

    【讨论】:

      猜你喜欢
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 2022-12-25
      • 1970-01-01
      • 1970-01-01
      • 2016-01-14
      • 1970-01-01
      • 2019-11-17
      相关资源
      最近更新 更多