将图像管道传输到FFmpeg stdin

时间:2018-12-17 11:37:50

标签: c# ffmpeg pipe

早上好,我需要通过文件夹将许多进入到文件夹中的图像流化为视频帧。我之前不知道它们有多少,所以我相信我需要保持FFmpeg打开并通过stdin传递它们。我已经读过这篇文章 create video from growing image sequence 然后我更改了c#代码以适合这样的问题:

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.ComponentModel;
namespace Example
{
public class Example
{

static void Main()
{

    AsyncMain().GetAwaiter().GetResult();
}

static async Task AsyncMain()
{
    Console.WriteLine("Press any button to quit.");
    var maintask = RunFFMPEG();
    var readtask = Task.Run(() => Console.Read());
    await Task.WhenAny(maintask, readtask);
}



static async Task RunFFMPEG()
{
    await Task.Run(() =>
    {
        const int fps = 30;
        const string outfile = "video_seg_%05d.ts";
        const string dir = @"C:\Users\funny\Desktop\in\";
        const string pattern = "{0}.bmp";
        const string path = dir + pattern;
        const string args = "-y -re -f image2pipe -framerate 2 -i - -c:v libx264 -r {0} -s 1920x1200 -b:v 256000 -flags +global_header -map 0 -f segment -segment_time 10 -segment_list_size 0 -segment_list list.m3u8 -segment_format mpegts {1}";
        const int startNum = 0;
        const int endNum = 100;

   var pinf = new ProcessStartInfo("ffmpeg", string.Format(args, fps, outfile));
        pinf.UseShellExecute = false;
        pinf.RedirectStandardInput = true;
        pinf.WorkingDirectory = dir;         

        Console.WriteLine("Starting ffmpeg...");
        var proc = Process.Start(pinf);
        using (var stream = new BinaryWriter(proc.StandardInput.BaseStream))
        {
            for (var i = startNum; i < endNum; i++)
            {
               var file = string.Format(path, i.ToString("D4"));
                System.Threading.SpinWait.SpinUntil(() => File.Exists(file) && CanReadFile(file));
                Console.WriteLine("Found file: " + file);
                stream.Write(File.ReadAllBytes(file));
            }
        }

 proc.WaitForExit();
        Console.WriteLine("Closed ffmpeg.");
    });
}


    static bool CanReadFile(string file)
    {
        //Needs to be able to read file
        FileStream fs = null;
        try
        {
            fs = File.OpenRead(file);
            return true;
        }
        catch (IOException)
        {
            return false;
        }
        finally
        {
            if (fs != null)
                fs.Close();
        }
    }


}
}

运行脚本时,出现以下屏幕:

Press any button to quit.
Starting ffmpeg...
ffmpeg version 4.1 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 8.2.1 (GCC) 20181017
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100

即使我的文件夹中有图像并且它们不断出现,它仍然保持这种状态。任何人都可以告诉我为什么不进行处理? (我尝试在固定数量的图像上单独运行FFmpeg命令,但不进行管道传输,因此可以正常工作。)

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法:基本上,由于路径输入错误,代码无法读取图像;我更改了行

var file = string.Format(path, i.ToString("D4"));

进入

var file = string.Format(string.Format(path,i), i.ToString("D4"));

它解决了问题。