【问题标题】:How to handle Python subprocess `FileNotFoundError: [WinError 2]`?如何处理 Python 子进程`FileNotFoundError: [WinError 2]`?
【发布时间】:2020-05-19 19:39:35
【问题描述】:

每当我尝试在 Windows 10 上运行此程序时,我都在努力管理出现的错误:

import ffmpeg_streaming
from ffmpeg_streaming import Formats, Bitrate, Representation, Size
import subprocess
import sys

def monitor(ffmpeg, duration, time_):
    per = round(time_ / duration * 100)
    sys.stdout.write("\rTranscoding...(%s%%) [%s%s]" % (per, '#' * per, '-' * (100 - per)))
    sys.stdout.flush()

video = ffmpeg_streaming.input('http://freja.hiof.no:1935/rtplive/_definst_/hessdalen03.stream/playlist.m3u8')

_360p = Representation(Size(640, 360), Bitrate(276 * 1024, 128 * 1024))
_480p = Representation(Size(854, 480), Bitrate(750 * 1024, 192 * 1024))
_720p = Representation(Size(1280, 720), Bitrate(2048 * 1024, 320 * 1024))

hls_stream = video.hls(Formats.h264(), hls_list_size = 1, hls_time = 60)
hls_stream.representations(_480p)
hls_stream.output('C:/Users/Documents/mashpy/VideoAnalytics_Mediaserver/ffmpeg_exec_test/video_fragments/hl_test.m3u8')

错误信息包含文本:

 Traceback (most recent call last):
  File "C:/Users/Documents/mashpy/VideoAnalytics_Mediaserver/ffmpeg_exec_test/capture.py", line 20, in <module>
    hls_stream.output('C:/Users/Documents/mashpy/VideoAnalytics_Mediaserver/ffmpeg_exec_test/video_fragments/hl_test.m3u8')
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\ffmpeg_streaming\_media.py", line 82, in output
    self._run(ffmpeg_bin, monitor, **options)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\ffmpeg_streaming\_media.py", line 93, in _run
    with Process(self, command_builder(ffmpeg_bin, self), monitor, **options) as process:
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\ffmpeg_streaming\_process.py", line 57, in __init__
    self.process = _p_open(commands, **options)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\ffmpeg_streaming\_process.py", line 28, in _p_open
    return subprocess.Popen(shlex.split(commands), **options)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] Specified file not found

不知道这里遗漏了什么。我主要设置了 PATH,相同的程序在 Ubuntu 18.04 上完美运行。你会怎么处理呢?

【问题讨论】:

  • 您确定地址正确吗? "C:Users/" 可能是一台计算机的情况,但对于另一台计算机,它可能是其他情况。
  • 我同时使用了正斜杠和反斜杠,都是一样的错误。
  • 会不会是模块没有正确安装?

标签: python python-3.x subprocess


【解决方案1】:

在最后一行的路径名前添加r

hls_stream.output(r'C:/Users/Documents/mashpy/VideoAnalytics_Mediaserver/ffmpeg_exec_test/video_fragments/hl_test.m3u8')

【讨论】:

  • 感谢您的回复。遗憾的是,这里似乎不是一个案例。错误仍然存​​在。
  • 如果您确定路径实际上应该有效。你可以试试 python os 包。 hls_stream.output(os.path.join(r'C:/Users/Documents/mashpy/VideoAnalytics_Mediaserver/ffmpeg_exec_test/video_fragments', 'hl_test.m3u8'))
  • 我也认为您的路径可能不完整。 'C:/Users/Documents' 应该是 'C:/Users/xxxx/Documents',其中 xxxx 是您的用户名。
猜你喜欢
  • 2017-08-22
  • 1970-01-01
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-06
  • 1970-01-01
相关资源
最近更新 更多