微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Ubuntu上的HandbrakeCLI出现Python错误,找不到文件

如何解决Ubuntu上的HandbrakeCLI出现Python错误,找不到文件

我正在尝试用python3编写一个程序,该程序在ubuntu机器上使用handbrakecli来重新编码一些视频。

我想念一些愚蠢的东西,但是我一辈子都想不起来,而且我已经花了无数个小时了。

这是代码

def convertvideo(input_file):
    hb_args = (" -i " + input_file + " -o " + handbraketempspace + " --preset-import-file "+handbrake_json_file_location + " -Z " + handbrake_profile_name)
    print (hb_args) # for debug only
    subprocess.Popen(handbrake + hb_args)
    return()

无论我如何重新排列“,我都会遇到两个错误之一,要么手刹说找不到文件,要么找不到我的预设。

这个确切的命令在命令行上可以完美地工作。

这是具体错误

Traceback (most recent call last):
  File "SubProcessing.py",line 161,in <module>
    finalvideo = convertvideo(sys.argv[2])
  File "SubProcessing.py",line 82,in convertvideo
    subprocess.Popen(handbrake + hb_args)
  File "/usr/lib/python3.8/subprocess.py",line 854,in __init__
    self._execute_child(args,executable,preexec_fn,close_fds,File "/usr/lib/python3.8/subprocess.py",line 1702,in _execute_child
    raise child_exception_type(errno_num,err_msg,err_filename)
FileNotFoundError: [Errno 2] No such file or directory: "/usr/bin/HandBrakeCLI -i /root/Alone-S02E01-Once_More_Unto_the_Breach_HDTV-720p.mkv -o /root/tmp/tempvideo.mkv --preset-import-file /mnt/media/PlexTestFiles/handbrake_presets.json -Z 'h.265_Hardware'"

在此先感谢您的帮助。再说一次,我不是python编码器,但我不认为这会很难。

解决方法

您需要使用数组而不是字符串中的参数调用subprocess.Popen。在您的情况下,相关部分为:

subprocess.Popen([handbrake,"-i",input_file,"-o",handbraketempspace,"--preset-import-file",handbrake_json_file_location,"-Z",handbrake_profile_name])

或者,您可以将shell=True设置为subprocess.Popen,但这会不必要地启动外壳程序,因此最好不要使用它。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。