从在线视频流中捕获屏幕截图

时间:2012-10-17 17:48:38

标签: c# video-streaming video-capture rtmp image-capture

我需要从rtmp或http视频流中捕获屏幕截图。我想每10秒捕获一次屏幕截图,并将其保存为png或jpg文件。

我已经能够找到任何为我这样做的程序,所以我正在考虑使用来自以下的lib在C#中编写应用程序: http://www.broccoliproducts.com/softnotebook/rtmpclient/rtmpclient.php

不幸的是,似乎rtmpClient lib只捕获rtmp流并将其保存到flv文件中,这不是我想要的。 谁知道任何更好的库可以帮助我这个?

4 个答案:

答案 0 :(得分:3)

我现在找到了解决问题的方法。如果有人想知道,我写了一个小程序,使用rtmpdump和ffmpeg来捕获图像。

    static void Main(string[] args)
    {
        const string rtmpDump = "rtmpdump.exe";
        const string rtmpDumpArguments = "-v -r rtmp://{stream} -o 1.flv -B 1";
        sRunExternalExe(rtmpDump, rtmpDumpArguments);

        const string ffmpeg = "ffmpeg.exe";
        const string ffmpegArguments = "-i 1.flv -ss 00:00:01 -an -r 1 -vframes 1 -s 400x300 -y 1.jpg";
        RunExternalExe(ffmpeg, ffmpegArguments);

        var theFile = new FileInfo("1.flv");
        if (theFile.Exists)
        {
            File.Delete("1.flv");
        }
    }


    public static string RunExternalExe(string filename, string arguments = null)
    {
        var process = new Process();

        process.StartInfo.FileName = filename;
        if (!string.IsNullOrEmpty(arguments))
        {
            process.StartInfo.Arguments = arguments;
        }

        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        process.StartInfo.UseShellExecute = false;

        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.RedirectStandardOutput = true;
        var stdOutput = new StringBuilder();
        process.OutputDataReceived += (sender, args) => stdOutput.Append(args.Data);

        string stdError = null;
        try
        {
            process.Start();
            process.BeginOutputReadLine();
            stdError = process.StandardError.ReadToEnd();
            process.WaitForExit();
        }
        catch (Exception e)
        {
            throw new Exception("OS error while executing " + Format(filename, arguments) + ": " + e.Message, e);
        }

        if (process.ExitCode == 0 || process.ExitCode == 2)
        {
            return stdOutput.ToString();
        }
        else
        {
            var message = new StringBuilder();

            if (!string.IsNullOrEmpty(stdError))
            {
                message.AppendLine(stdError);
            }

            if (stdOutput.Length != 0)
            {
                message.AppendLine("Std output:");
                message.AppendLine(stdOutput.ToString());
            }

            throw new Exception(Format(filename, arguments) + " finished with exit code = " + process.ExitCode + ": " + message);
        }
    }

    private static string Format(string filename, string arguments)
    {
        return "'" + filename +
            ((string.IsNullOrEmpty(arguments)) ? string.Empty : " " + arguments) +
            "'";
    }

答案 1 :(得分:2)

你可以使用bash如果你在Linux上(这可能不适用于你,但希望能帮助任何人谷歌搜索),这就是我使用'rtmpdump'将它用于我们的流媒体产品的方式(apt-get install rtmpdump ):

/脚本/ rtmpsnapshot

#!/bin/bash
rtmpdump --live --timeout=9 -r $1 -a $2 -y $3  --stop 1 -o - | avconv -i - -s 720x404 -vframes 1 $4

这样称呼:

/scripts/rtmpsnapshot rtmp://myserver.com/applicationname/ applicationname streamname /tmp/mysnapshot.jpg

答案 2 :(得分:0)

你看过SnagIt! v11.1了吗?我刚升级了我的副本,它将捕获视频流和相关的音频。

我不知道每隔10秒就有一次屏幕截图,但我知道它内置了计时器功能,并能够隔离各个帧。

可能值得一看。我认为它有30天的试用期。

答案 3 :(得分:-3)

如果您使用的是Windows 7,则会附带一个可捕获屏幕的代码段工具。