C # uses FFmpeg to push the stream, and gets the stream to be saved locally, ready for the media to play!

Source: Internet
Author: User

Recently developed a C # based pusher has not been ideal, and finally after unremitting efforts to study a bit of results, this way to make a note; This article focuses on how to use FFmpeg to carry out a simple push flow, seemingly simple few lines of code is not official document very laborious. And get the source of the stream: the following →

  #region  rtmp Push Stream (* * successfully pushed to server * *)  Network.create (). Withsource (InputPath)  //  The InputPath can be changed to get the video stream of the device . Withdest ( " rtmp://192.168.61.128/live/ Livestream   ") //  The RTMP server address  can be updated to meet your needs. Withfilter (new  x264filter {constantquantizer = 20  }). Withfilter ( new   Resizefilter ( resolution.x720p)).            Push ();   #endregion  
            network.create ()                . Withsource ("rtmp://192.168.61.128/live/livestream")//  InputPath can be changed to get the video stream of the device                . Withdest (InputPath)// This path can be changed freely, if it is live, you do not need to use this path, direct reading stream to the player to play real-time reception.                 . Withfilter (new  )                . Withfilter (new  resizefilter (resolution.x720p))                . Pull ();

The above is the core code that pushes the stream and gets the stream stored locally.

1: First establish the connection between the server and the client;

2: Initialize the server address;

3: Initialize the path;

4: Video property settings;

5: Push/Lahue operation;

  /// <summary>        ///push Stream to rtmp server/// </summary>         Public voidPush () {Validate (); if(_desttype! =targettype.live) {Throw NewApplicationException ("when streaming to the rtmp server, the source type must be the ' rtmptype.live ' type."); }            //when the argument is false, the push-flow            var@params = Getparams (false);        Processor.ffmpeg (@params); }
  /// <summary>        ///pull stream from rtmp server-read video data ==pull a stream from rtmp server/// </summary>         Public voidPull () {Validate (); if(! Testrtmpserver (_source,true))                Throw NewApplicationException ("The rtmp server sent an error."); if(_sourcetype! =targettype.live) {Throw NewApplicationException ("must be an rtmp server."); }            //when the parameter is true, the video stream is read            var@params = Getparams (false);        Processor.ffmpeg (@params); }
 /// <summary>        ///detecting output input sources and filters/// </summary>        Private voidValidate () {if(_sourcetype = =Targettype.default)Throw NewApplicationException ("source error. Please enter the source! "); if(_desttype = =Targettype.default)Throw NewApplicationException ("dest Error. Please enter a dest"); varSupportfilters =New[] {"Resize","Segment","X264","audiorate","audiobitrate" }; if(_filters. Any (x =!supportfilters.contains (x.name))) {                Throw NewApplicationException (string. Format ("Filter not supported, filter only supported: {0} type", Supportfilters.aggregate (string. Empty, (current, filter) = + Current + (filter +",")). TrimEnd (New[] {',' }))); }        }

This is the method used to push the flow, Validate () → This method is mainly used to detect the output input source and filter, and then detect whether the state of the input stream is a file (files or live), and finally call FFmpeg to process the input parameter method.

Similarly, getting a stream is also true.

The following shows the effect: (after the push stream succeeds, it will have the effect as shown, and can call the third-party player such as DirectShow or its own ffplay to play)

Next is the effect of this pull stream: When the read is not completed in the figure

It is worth noting that the reception is starting from where you started to receive, the video pushes the stream is based on a frame one frame push, and we receive the stream is a frame of the reception, so the figure is actually played to the end of the video, so as to prevent the video repeated upload, repeat download.

C # uses FFmpeg to push the stream, and gets the stream to be saved locally, ready for the media to play!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.