简体   繁体   English

OSError: [Errno 63] 文件名太长

[英]OSError: [Errno 63] File name too long

I have a short script that needs to merge multiple MKV files into one every night.我有一个简短的脚本,需要每天晚上将多个 MKV 文件合并为一个。

import os, glob
import subprocess

concatenated_files = ""

os.chdir("/cams/")
for index, file in enumerate(glob.glob("*.mkv")):
    if index == 0:
        concatenated_files = file
    else:
        concatenated_files += " + " + file

# print(concatenated_files)

returncode = subprocess.call("mkvmerge -o out.mkv " + concatenated_files)

I am getting the following error with just a few files我收到以下错误,只有几个文件

OSError: [Errno 63] File name too long: 'mkvmerge -o out.mkv video21-06-28_09-12-08-51.mkv + video21-06-28_07-55-36-80.mkv + video21-06-28_09-52-05-79.mkv + video21-06-28_08-47-56-69.mkv + video21-06-28_09-15-04-34.mkv + video21-06-28_09-32-43-25.mkv

I am planning to merge hundreds of them, so not really sure how to sort this situation with Python.我计划合并数百个,所以不太确定如何使用 Python 对这种情况进行排序。 It works well from the shell.它从外壳运行良好。

subprocess.call() first argument "args" is a list . subprocess.call() 第一个参数“args”是一个列表

So I think it should probably be like this:所以我想大概应该是这样的:

  import os, glob
  import subprocess

  concatenated_files = ""

  os.chdir("/cams/")
  args = ["mkvmerge"]
  for index, file in enumerate(glob.glob("*.mkv")):
     args.append(file)


  args.append("-o")        
  args.append("out.mkv")        


  returncode = subprocess.call(args)

Maybe using a shell script with only three lines of code is more simple and more clear:也许使用只有三行代码的shell脚本更简单明了:

ls /cams/ | grep -E ".+\.mkv$" | while read filename; do
  filenames="${filenames} ${filename}"
done
mkvmerge -o out.mkv ${filenames}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM