Пример #1
0
        /// <summary>
        /// Listens for a request on the given port and begins streaming MJPEG images when a client connects. Requires raw RGB24 frames as input.
        /// </summary>
        /// <param name="width">The width of the raw frames. Defaults to 640.</param>
        /// <param name="height">The height of the raw frames. Defaults to 480.</param>
        /// <param name="fps">Expected FPS of the raw frames. Defaults to 24.</param>
        /// <param name="listenPort">The port to listen on. Defaults to 8554.</param>
        /// <param name="echoOutput">Whether to echo stdout and stderr to the console or suppress it. Defaults to true.</param>
        /// <param name="maxBitrate">Maximum output bitrate. If source data is available at a higher bitrate, VLC caps to this. Defaults to 2500 (25Mbps).</param>
        /// <param name="maxFps">Maximum output framerate. If source data is available at a higher framerate, VLC caps to this. Defaults to 20.</param>
        /// <returns>An initialized instance of <see cref="ExternalProcessCaptureHandler"/></returns>
        public static ExternalProcessCaptureHandler StreamRawRGB24asMJPEG(int width = 640, int height = 480, int fps = 24, int listenPort = 8554, bool echoOutput = true, int maxBitrate = 2500, int maxFps = 20)
        {
            var opts = new ExternalProcessCaptureHandlerOptions
            {
                Filename           = "/bin/bash",
                EchoOutput         = true,
                Arguments          = $"-c \"ffmpeg -hide_banner -f rawvideo -c:v rawvideo -pix_fmt rgb24 -s:v {width}x{height} -r {fps} -i - -f h264 -c:v libx264 -preset ultrafast -tune zerolatency -vf format=yuv420p - | cvlc stream:///dev/stdin --sout '#transcode{{vcodec=mjpg,vb={maxBitrate},fps={maxFps},acodec=none}}:standard{{access=http{{mime=multipart/x-mixed-replace;boundary={_VLCInternalMimeBoundaryName}}},mux=mpjpeg,dst=:{listenPort}/}}' :demux=h264\"",
                DrainOutputDelayMs = 500, // default = 500
                TerminationSignals = ExternalProcessCaptureHandlerOptions.SignalsFFmpeg
            };

            return(new ExternalProcessCaptureHandler(opts));
        }
Пример #2
0
        /// <summary>
        /// Listens for a request on the given port and begins streaming MJPEG images when a client connects. Requires h.264 encoded I420 (YUV420p) as input.
        /// </summary>
        /// <param name="listenPort">The port to listen on. Defaults to 8554.</param>
        /// <param name="echoOutput">Whether to echo stdout and stderr to the console or suppress it. Defaults to true.</param>
        /// <param name="maxBitrate">Maximum output bitrate. If source data is available at a higher bitrate, VLC caps to this. Defaults to 2500 (25Mbps).</param>
        /// <param name="maxFps">Maximum output framerate. If source data is available at a higher framerate, VLC caps to this. Defaults to 20.</param>
        /// <returns>An initialized instance of <see cref="ExternalProcessCaptureHandler"/></returns>
        public static ExternalProcessCaptureHandler StreamH264asMJPEG(int listenPort = 8554, bool echoOutput = true, int maxBitrate = 2500, int maxFps = 20)
        {
            var opts = new ExternalProcessCaptureHandlerOptions
            {
                Filename           = "cvlc",
                Arguments          = $"stream:///dev/stdin --sout \"#transcode{{vcodec=mjpg,vb={maxBitrate},fps={maxFps},acodec=none}}:standard{{access=http{{mime=multipart/x-mixed-replace;boundary={_VLCInternalMimeBoundaryName}}},mux=mpjpeg,dst=:{listenPort}/}}\" :demux=h264",
                EchoOutput         = echoOutput,
                DrainOutputDelayMs = 500, // default
                TerminationSignals = ExternalProcessCaptureHandlerOptions.SignalsVLC
            };

            return(new ExternalProcessCaptureHandler(opts));
        }
        /// <summary>
        /// Streams video from the standard output stream via FFmpeg to an RTMP server.
        /// </summary>
        /// <param name="streamName">The meta name of the stream.</param>
        /// <param name="streamUrl">The url of your RTMP server - the url to stream to.</param>
        /// <param name="echoOutput">Whether to echo stdout and stderr to the console or suppress it. Defaults to true.</param>
        /// <returns>An initialized instance of <see cref="ExternalProcessCaptureHandler"/></returns>
        public static ExternalProcessCaptureHandler RTMPStreamer(string streamName, string streamUrl, bool echoOutput = true)
        {
            var opts = new ExternalProcessCaptureHandlerOptions
            {
                Filename           = "ffmpeg",
                Arguments          = $"-i - -vcodec copy -an -f flv -metadata streamName={streamName} {streamUrl}",
                EchoOutput         = echoOutput,
                DrainOutputDelayMs = 500, // default
                TerminationSignals = ExternalProcessCaptureHandlerOptions.SignalsFFmpeg
            };

            return(new ExternalProcessCaptureHandler(opts));
        }
        /// <summary>
        /// Transcodes the standard output stream via FFmpeg into an MP4 format with options that create a "fragmented" MP4 (more keyframes than usual, and no trailing
        /// MOOV atom trailing-header). Because FFmpeg doesn't do a clean shutdown when running as a child process, it is currently unable to output a "clean" standard
        /// MP4 without these settings, it will not generate the final MOOV trailing-header. This also means the fragmented MP4 may lose a couple seconds at the end
        /// of the final video.
        /// </summary>
        /// <param name="directory">The directory to store the output video file.</param>
        /// <param name="filename">The name of the video file.</param>
        /// <param name="echoOutput">Whether to echo stdout and stderr to the console or suppress it. Defaults to true.</param>
        /// <param name="bitrate">Output bitrate. Defaults to 2500 (25Mbps).</param>
        /// <param name="fps">Output framerate. Defaults to 24.</param>
        /// <returns>An initialized instance of <see cref="ExternalProcessCaptureHandler"/></returns>
        public static ExternalProcessCaptureHandler RawVideoToMP4(string directory, string filename, bool echoOutput = true, int bitrate = 2500, int fps = 24)
        {
            System.IO.Directory.CreateDirectory(directory);

            var opts = new ExternalProcessCaptureHandlerOptions
            {
                Filename           = "ffmpeg",
                Arguments          = $"-framerate {fps} -i - -b:v {bitrate}k -c copy -movflags +frag_keyframe+separate_moof+omit_tfhd_offset+empty_moov {directory.TrimEnd()}/{filename}.mp4",
                EchoOutput         = echoOutput,
                DrainOutputDelayMs = 500, // default
                TerminationSignals = ExternalProcessCaptureHandlerOptions.SignalsFFmpeg
            };

            return(new ExternalProcessCaptureHandler(opts));
        }
        /// <summary>
        /// Records video from the standard output stream via FFmpeg, forcing it into an avi container that can be opened by media players without explicit command line flags.
        /// </summary>
        /// <param name="directory">The directory to store the output video file.</param>
        /// <param name="filename">The name of the video file.</param>
        /// <param name="echoOutput">Whether to echo stdout and stderr to the console or suppress it. Defaults to true.</param>
        /// <returns>An initialized instance of <see cref="ExternalProcessCaptureHandler"/></returns>
        public static ExternalProcessCaptureHandler RawVideoToAvi(string directory, string filename, bool echoOutput = true)
        {
            System.IO.Directory.CreateDirectory(directory);

            var opts = new ExternalProcessCaptureHandlerOptions
            {
                Filename           = "ffmpeg",
                Arguments          = $"-i - -c:v copy -an -f avi -y {directory.TrimEnd()}/{filename}.avi", // -re option should not be specified here, it's meant to rate-limit scenarios like streaming a pre-recorded file; see: https://stackoverflow.com/a/48497672/152997
                EchoOutput         = echoOutput,
                DrainOutputDelayMs = 500,                                                                  // default
                TerminationSignals = ExternalProcessCaptureHandlerOptions.SignalsFFmpeg
            };

            return(new ExternalProcessCaptureHandler(opts));
        }