首页 > 解决方案 > Python3 子进程“拒绝访问”

问题描述

我正在尝试通过带有大量参数的命令行运行 VLC,因为我正在将采集卡中的原始视频编码到传输流中:

import os
import sys
import subprocess

comm_string = r'"G:\VLC\vlc" --ffmpeg-hw --avcodec-hw=any dshow:// :dshow-vdev="Video (00 Pro Capture HDMI 4K+)" :dshow-adev="Audio ' \
'(2- 00 Pro Capture HDMI 4K+)" :dshow-threads=8 :dshow-aspect-ratio=16\:9 :dshow-size="3840x2160" :dshow-pixel_format=yuv444p16le ' \
':dshow-tune=film :dshow-preset=lossless :dshow-profile=main10 show-vcodec=x265 :dshow-fps=50 :dshow-crf=0 :dshow-acodec=mp4a ' \
':dshow-stereo-mode=5 :dshow-force-surround-sound=0 :dshow-ab=128 :dshow-samplerate=44100 :no-dshow-config :live-caching=300 --sout ' \
'"#transcode{venc=ffmpeg,vcodec=mp2v,threads=8,aspect=16:9,width=3840,height=2160,fps=50,acodec=a52,ab=1500,channels=2,samplerate=48000,soverlay}' \
':standard{access=file,dst=-,mux=ts}"'


startupinfo = None
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

process = subprocess.Popen([comm_string], startupinfo=startupinfo)

...这会产生以下错误:

Traceback (most recent call last):
  File "G:/HTPC Scripts/NPVR Command Line/Command Line.py", line 17, in <module>
    process = subprocess.Popen([comm_string], startupinfo=startupinfo)
  File "G:\Python36\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "G:\Python36\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
PermissionError: [WinError 5] Access is denied

...我的理解是,您可以使用单个字符串将程序及其所有伴随参数传递到子进程中。不是这样吗?如果没有,我应该如何修改我的语法?

谢谢

标签: python-3.xsubprocess

解决方案


这是解决方案顺便说一句:

import os
import sys
import subprocess

os.chdir('G:\\VLC\\')

startupinfo = None
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

process = subprocess.Popen([
'vlc',
'--ffmpeg-hw',
'--avcodec-hw=any',
'dshow:// :dshow-vdev="Video (00 Pro Capture HDMI 4K+)" :dshow-adev="Audio (2- 00 Pro Capture HDMI 4K+)"',
':dshow-threads=8',
':dshow-aspect-ratio=16\:9',
':dshow-size="3840x2160"',
':dshow-pixel_format=yuv444p16le',
':dshow-tune=film',
':dshow-preset=lossless',
':dshow-profile=main10',
'show-vcodec=x265',
':dshow-fps=50',
':dshow-crf=0',
':dshow-acodec=mp4a',
':dshow-stereo-mode=5',
':dshow-force-surround-sound=0',
':dshow-ab=128',
':dshow-samplerate=44100',
':no-dshow-config',
':live-caching=300',
'--sout',
'"#transcode{venc=ffmpeg,vcodec=mp2v,threads=8,aspect=16:9,width=3840,height=2160,fps=50,acodec=a52,ab=1500,channels=2,samplerate=48000,soverlay}:standard{access=file,dst=-,mux=ts}"',
], stdout=subprocess.PIPE, startupinfo=startupinfo)

推荐阅读