从 windows 服务执行的子进程使用 DirectX

Using DirectX from subprocess executed by windows service

我需要从 windows 服务执行 ffmpeg 进程并捕获它的标准输出。在我使用硬件加速之前它工作正常。由于windows服务访问DirectX被限制,子进程也无法访问。

当我从控制台应用程序执行相同的代码时,一切正常,但是从 windows 服务执行的相同代码无法使用硬件加速。

        string ffmpegArgs = /*-hwaccel dxva2 */"-threads 0 -probesize 100512 -i c:/Temp/test.mp4 -vf yadif -vcodec libx264 -preset ultrafast -tune zerolatency -profile baseline -x264-params keyint=20:min-keyint=20:scenecut=-1 -acodec aac -b:a 48k -flags +cgop -f mp4 -movflags empty_moov+default_base_moof+frag_keyframe c:/temp/output.avi";

        var psi = new ProcessStartInfo
        {
            FileName = "c:/Temp/ffmpeg4/ffmpeg.exe",
            Arguments = ffmpegArgs,
            WorkingDirectory = "c:/Temp/ffmpeg4",
            CreateNoWindow = false,
            WindowStyle = ProcessWindowStyle.Hidden,
            RedirectStandardInput = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false
        };

        var processVideo = new Process { StartInfo = psi }.Start();

我需要以某种方式打破继承的限制,以便能够使用硬件加速执行 ffmpeg(访问 DirectX API)。有什么建议吗?

如果您在“服务”对话框中右键单击该服务 -> 属性 -> 登录,您应该会看到如下内容:

选中复选框 "Allow Service to interact with desktop" 这应该使您能够访问这些 "restricted" API

您可以指示FFMPEG进程使用DirectX 11 Hardware Video acceleration。 在这种情况下,FFMPEG 将需要创建一个 DirectX 11 device, by calling D3D11CreateDevice 函数,它不需要提供任何 window 句柄。

为了使 FFMPEG 使用 DirectX 11 硬件视频加速,您需要改为指定以下 hwaccel 参数:

-hwaccel d3d11va