How to open streaming media in FFmpeg class library (pass parameters)

Source: Internet
Author: User

When developing with the FFmpeg class library, the function that opens the streaming media (or local file) is Avformat_open_input ().

If you open the network stream, you should precede the function avformat_network_init ().

In general, you can just pass in the URL of the streaming media. However, when you open some streaming media, you may need to attach some parameters.

For example, when playing a China National radio sound signal, its URL is "rtsp://mms.cnr.cn/cnr003?" mze5mtg0izejizi5njgwoq== "

If opening directly is not successful, we can use Ffplay to do the experiment:

    1. Ffplay rtsp://mms.cnr.cn/cnr003? mze5mtg0izejizi5njgwoq==
An error will occur:

Invalid data found when processing input

At this point we need to specify that it is transmitted as TCP, and the command needs to be changed to the following form:

    1. Ffplay-rtsp_transport TCP rtsp://mms.cnr.cn/cnr003? mze5mtg0izejizi5njgwoq==

After attaching the parameters, the discovery will play normally.

In addition, you can attach some parameters, such as

    1. Ffplay-rtsp_transport tcp-max_delay 5000000 rtsp://mms.cnr.cn/cnr003? mze5mtg0izejizi5njgwoq==

When programming with the FFmpeg class library, how do you pass these additional parameters to Avformat_open_input ()? After study, it is found that the parameters can be passed to Avformat_open_input () by Avdictionary.

Take a look at the definition of Avformat_open_input ():

  1. /**
  2. * Open an input stream and read the header. The codecs is not opened.
  3. * The stream must is closed with av_close_input_file ().
  4. *
  5. * @param PS Pointer to user-supplied avformatcontext (allocated by Avformat_alloc_context).
  6. * May being a pointer to NULL, in which case an avformatcontext was allocated by this
  7. * Function and written into PS.
  8. * Note that a user-supplied Avformatcontext is freed on failure.
  9. * @param filename Name of the stream to open.
  10. * @param fmt If non-null, this parameter forces a specific input format.
  11. * Otherwise the format is autodetected.
  12. * @param options A dictionary filled with avformatcontext and demuxer-private options.
  13. * On return this parameter'll be destroyed and replaced with a dict containing
  14. * Options that were not found. May is NULL.
  15. *
  16. * @return 0 on success, a negative averror on failure.
  17. *
  18. * @note If want to use custom IO, preallocate the format context and set its PB field.
  19. */
  20. int Avformat_open_input (Avformatcontext **ps, const char *filename, Avinputformat *fmt, Avdictionary **options)  ;
/** * Open an input stream and read the header. The codecs is not opened. * The stream must is closed with av_close_input_file (). * * @param PS Pointer to user-supplied avformatcontext (allocated by Avformat_alloc_context). * May being a pointer to NULL, in which case an avformatcontext was allocated by this * function and Writt En to PS. * Note that a user-supplied avformatcontext would be freed on failure. * @param filename Name of the stream to open. * @param fmt If non-null, this parameter forces a specific input format. * Otherwise the format is autodetected. * @param options A dictionary filled with avformatcontext and demuxer-private options.  * On return this parameter is destroyed and replaced with a dict containing * options That's were not found. May is NULL. * * @return 0 on success, a negative averror on failure. * * @note If want to use custom IO, preallocate the format context and set its PB field. */int avformat_open_input (avformatcontext **ps, const char *filename, Avinputformat *fmt, avdictionary **options); 


You can see that the 4th parameter of Avformat_open_input () is a parameter of type avdictionary. This parameter is the additional parameter passed in.

Av_dict_set () is used when setting the avdictionary.

Here's a look at the command

    1. Ffplay-rtsp_transport tcp-max_delay 5000000 rtsp://mms.cnr.cn/cnr003? mze5mtg0izejizi5njgwoq==

How to translate to code implementation:

  1. Avformatcontext *pformatctx;
  2. Pformatctx = Avformat_alloc_context ();
  3. ... Code slightly
  4. Avdictionary *avdic=null;
  5. Char option_key[]="Rtsp_transport";
  6. Char option_value[]="TCP";
  7. Av_dict_set (&avdic,option_key,option_value,0);
  8. Char option_key2[]="Max_delay";
  9. Char option_value2[]="5000000";
  10. Av_dict_set (&avdic,option_key2,option_value2,0);
  11. Char url[]="rtsp://mms.cnr.cn/cnr003?  mze5mtg0izejizi5njgwoq== ";
  12. Avformat_open_input (&pformatctx,url,null,&avdic);

How to open streaming media in FFmpeg class library (pass parameters)

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.