Development of streaming media based on FFmpeg SDK 1---Decoding media file stream information

Source: Internet
Author: User

Recent projects related to the development of streaming media, because of the development experience know its difficulty, no way can only be picked up, the latest version of the SDK was changed a mess, but the general development of ideas are the same, see how many books to check how much information is useless, step by step to write code is the key to learn.

I will pass every day of learning, updated to the blog post, I hope to give more people want to learn to bring help, the end of the article attached to the project and the latest version of the SDK.

FFmpeg is used by most people to command the line, in fact, in real streaming media development, in order to flexibly use its development of streaming media application sequence, must use the official SDK development, in fact, we have a lot of products on the market

are based on ffmpeg, such as XX Av.

FFmpeg official website http://www.ffmpeg.org/

API Address http://www.ffmpeg.org/doxygen/trunk/index.html

Because the compilation in Windows is very painful, so it is recommended to download the compiled binaries directly, note that the official web site does not have a direct complete development package, you need to remove the download Linux or Windows

Shared libraries for Windows also need to download. lib Import library, since I am windows here I will provide windows

http://ffmpeg.zeranoe.com/builds/This page can be downloaded to the dynamic library and to the storage. Because FFmpeg has been entrusted to other organizations to maintain ... Find it on the page below there is also a point is that since the use of other people's things to remember must follow the LGPL or GPL license ... Don't embarrass your countrymen.

That's what foreigners say.

Donating shows that's benefit from my work and is thankful for the time I spend on it. So if you like my work and would like to see more, feel free to donate, if you can ' t right now don ' t worry about it and Ju St enjoy using FFmpeg on Windows. Thank all who have donated in the past!

Specific no nonsense, how to configure the project what, this is the novice level of the problem, I will not elaborate directly on the code plus comments I will provide source code download ... Project configuration Well, everyone, download the research.

[CPP]View Plaincopyprint?
  1. Ffmpeg_test.cpp: Defines the entry point of the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <windows.h>
  5. #ifdef _cpprtti
  6. extern "C"
  7. {
  8. #endif
  9. #include "libavcodec/avcodec.h"//Codec
  10. #include "libavformat/avformat.h"//format context
  11. #include "libavformat/avio.h"//audio/Video IO
  12. #include "libavutil/file.h"//Processing files
  13. #ifdef _cpprtti
  14. };
  15. #endif
  16. void Setstdclr (WORD wd)
  17. {
  18. Setconsoletextattribute (:: GetStdHandle (Std_output_handle), WD);
  19. }
  20. int _tmain (int argc, _tchar* argv[])
  21. {
  22. //Register all encoder parser binary stream filters
  23. Av_register_all ();
  24. Avcodec_register_all ();
  25. SETSTDCLR (foreground_red | Foreground_green);
  26. Avformatcontext *pcontext=null; //Format Context
  27. int errno=0;
  28. Pcontext=avformat_alloc_context ();
  29. //Open input File New interface
  30. if (0==avformat_open_input (&pcontext,". \\test.mp4", Nullptr,null)) {
  31. printf ("Open file input succeeded!\n");
  32. }Else
  33. return 0;
  34. //Retrieve flow information from context
  35. if (0==avformat_find_stream_info (pcontext,null))
  36. {
  37. printf ("Get flow information successfully!\n");
  38. }Else
  39. return 0;
  40. //Loop multiple streams
  41. SETSTDCLR (foreground_red | Foreground_blue);
  42. For (unsigned int i=0;i<pcontext->nb_streams;i++)
  43. {
  44. //Media streaming
  45. Avstream *pstream = pcontext->streams[i];
  46. //Frame rate information is rational number/irrational number
  47. Avrational Frame =pstream->r_frame_rate;
  48. //Time ratio units
  49. Avrational timebase = pstream->time_base;
  50. //Stream duration bit rate
  51. int64_t duration= pstream->duration;
  52. printf ("Media Duration%d\n", duration);
  53. //Get the encoding type
  54. Avcodeccontext *pcodeccontext=pstream->codec;
  55. //Get Media type
  56. /************************************************************************/  
  57. /*  
  58. Enum Avmediatype {
  59. Avmedia_type_unknown =-1,///< usually treated as Avmedia_type_data
  60. Avmedia_type_video,
  61. Avmedia_type_audio,
  62. Avmedia_type_data,///< Opaque DATA information usually continuous
  63. Avmedia_type_subtitle,
  64. Avmedia_type_attachment,///< Opaque data information usually sparse
  65. Avmedia_type_nb
  66. };
  67. */
  68. /************************************************************************/  
  69. Avmediatype avmediatype=pcodeccontext->codec_type;
  70. //Encoder ID
  71. Avcodecid codecid=pcodeccontext->codec_id;
  72. if (Avmediatype = = Avmedia_type_audio)
  73. {
  74. //If the video
  75. int audiochannels = pcodeccontext->channels;
  76. int samplerate = pcodeccontext->sample_rate;
  77. PixelFormat PixelFormat = pcodeccontext->pix_fmt;
  78. printf ("stream%d audio \ n", i);
  79. printf ("Audio sampling frequency%d/%d\n", Timebase.num,timebase.den);
  80. printf ("Audio time Unit%d/%d\n", Timebase.num,timebase.den);
  81. printf ("Number of audio channels%d\n", audiochannels);
  82. }
  83. Else if (Avmediatype = = avmedia_type_video)
  84. {
  85. //If the audio
  86. int videowidth = pcodeccontext->width;
  87. int videoheight = pcodeccontext->height;
  88. Avsampleformat samplefmt = pcodeccontext->sample_fmt;
  89. printf ("stream%d video \ n", i);
  90. printf ("frame rate frame rate%d/%d\n", frame.den,frame.num);
  91. printf ("Video time Unit%d/%d\n", Timebase.num,timebase.den);
  92. printf ("image width:%d\t height:%d\t%\n", videowidth,videoheight);
  93. printf ("image width:%d\t height:%d\t%\n", videowidth,videoheight);
  94. }
  95. switch (codecid)
  96. {
  97. Case AV_CODEC_ID_AAC:
  98. printf ("encoder faac\n");
  99. Break ;
  100. Case av_codec_id_h264:
  101. printf ("encoder h264\n");
  102. Break ;
  103. }
  104. }
  105. //Release context
  106. if (!pcontext)
  107. {
  108. Avformat_close_input (&pcontext);
  109. }
  110. return 0;
  111. }


The results of the operation are as follows:

Engineering

http://download.csdn.net/detail/yue7603835/8268095

FFmpeg SDK based streaming media Development 1---Decoding media file stream information (GO)

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.