我正在嘗試用 python3 編寫一個程序,在我的 ubuntu 機(jī)器上使用 handbrakecli 來重新編碼一些視頻。我錯過了一些愚蠢的東西,但我一生都無法弄清楚這一點(diǎn),而且我已經(jīng)為此花費(fèi)了無數(shù)個小時。這是代碼: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()無論我如何重新排列“,我都會收到兩個錯誤之一,要么手剎說找不到文件,要么找不到我的預(yù)設(shè)。這個確切的命令可以在命令行中完美運(yùn)行。這是具體的錯誤: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 編碼員,但我不認(rèn)為這會這么難。
1 回答

Qyouu
TA貢獻(xiàn)1786條經(jīng)驗 獲得超11個贊
您需要subprocess.Popen
使用數(shù)組中的參數(shù)進(jìn)行調(diào)用,而不是字符串。在您的情況下,相關(guān)部分將是:
subprocess.Popen([handbrake, "-i", input_file, "-o", handbraketempspace, "--preset-import-file", handbrake_json_file_location, "-Z", handbrake_profile_name])
或者,您可以指定shell=True
to subprocess.Popen
,但這會不必要地啟動 shell,因此最好不要使用它。
添加回答
舉報
0/150
提交
取消