myt*_*h0s 3 ffmpeg file-conversion converters python-3.x
我有很多 .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
Run Code Online (Sandbox Code Playgroud)
嗯,答案总是比你想象的要简单。
归结为:
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
Run Code Online (Sandbox Code Playgroud)
确保 ffmpeg.exe 与脚本位于同一目录中。
| 归档时间: |
|
| 查看次数: |
16424 次 |
| 最近记录: |