• 解封装_播放


    1. #include
    2. #include
    3. #include "xvideo_view.h"
    4. #include "xdecode.h"
    5. using namespace std;
    6. extern "C"{
    7. //引用ffmpeg头文件
    8. #include
    9. }
    10. //预处理治理导入库
    11. #pragma comment(lib, "avformat.lib")
    12. #pragma comment(lib, "avutil.lib")
    13. #pragma comment(lib, "avcodec.lib")
    14. #define CERR(err) if (err) \
    15. { \
    16. PrintErr(err); \
    17. return -1; \
    18. }
    19. int main(int argc, char* argv[])
    20. {
    21. //打开媒体文件
    22. const char* url = "v1080.mp4";
    23. //解封装上下文
    24. AVFormatContext* ic = nullptr;
    25. int re = avformat_open_input(&ic, url,
    26. NULL, //封装器格式,NULL表示自动探测 根据后缀面或者文件头
    27. NULL // 参数设置,rtsp需要设置
    28. );
    29. CERR(re);
    30. //获取媒体信息,无头部格式
    31. avformat_find_stream_info(ic, NULL);
    32. CERR(re);
    33. //打印封装信息
    34. av_dump_format(ic,1, url,
    35. 0 //0表示上下文是输入, 1表示上下文是输出
    36. );
    37. AVStream* as = nullptr;
    38. AVStream* vs = nullptr;
    39. for (int i = 0; i < ic->nb_streams; i++)
    40. {
    41. if (ic->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
    42. {
    43. as = ic->streams[i];
    44. cout << "========音频=======" << endl;
    45. cout << "sample_rate:" << as->codecpar->sample_rate << endl;
    46. }
    47. else if (ic->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
    48. {
    49. vs = ic->streams[i];
    50. cout << "========视频=======" << endl;
    51. cout << "width:" << vs->codecpar->width << endl;
    52. cout << "height:" << vs->codecpar->height << endl;
    53. }
    54. }
    55. auto video_codec_id = vs->codecpar->codec_id; //视频编码器ID
    56. AVCodecParameters* video_par = vs->codecpar; //视频编码参数
    57. ///
    58. ///视频解码器初始化
    59. XDecode decode;
    60. AVCodecContext* decode_c = XCodec::Create(video_codec_id, false);
    61. //解封装的视频编码参数,传递给解码上下文
    62. avcodec_parameters_to_context(decode_c, video_par);
    63. //设置到解码器中,线程安全,设置完后, decode_c 不在类外部使用
    64. decode.set_c(decode_c);
    65. if (!decode.Open())
    66. {
    67. cout << "decode.Open() error" << endl;
    68. return -1;
    69. }
    70. //创建解码输出的空间
    71. AVFrame* frame = decode.CreateFrame();
    72. //
    73. //
    74. ///渲染初始化
    75. //
    76. XVideoView* view = XVideoView::Create();
    77. view->Init(video_par->width, video_par->height, (XVideoView::Format)video_par->format);
    78. AVPacket pkt;
    79. for (;;)
    80. {
    81. re = av_read_frame(ic, &pkt);
    82. CERR(re);
    83. if (vs && pkt.stream_index == vs->index)
    84. {
    85. cout << "视频:" ;
    86. //解码视频
    87. if (decode.Send(&pkt))
    88. {
    89. while (decode.Recv(frame))
    90. {
    91. cout << frame->pts << " " << flush;
    92. view->DrawFrame(frame); //渲染
    93. }
    94. }
    95. }
    96. else if (as && pkt.stream_index == as->index)
    97. {
    98. //cout << "音频:";
    99. }
    100. //cout << pkt.pts << ":" << pkt.dts << ":" << pkt.size << endl;
    101. av_packet_unref(&pkt);
    102. //this_thread::sleep_for(100ms);
    103. }
    104. av_frame_free(&frame);
    105. avformat_close_input(&ic);
    106. return 0;
    107. }

  • 相关阅读:
    对数似然函数 | 交叉熵 | 损失函数
    拓端tecdat|R语言泊松Poisson回归模型分析案例
    【SQL Server】入门教程-基础篇(完结)
    如何让本地局域网IP端口映射到域名
    iNFTnews | 回顾支付宝鲸探发展历程:是数藏的投机客还是引领者?
    Linux系统下建立Socket聊天服务器
    java成神之路-基础篇
    Unity项目升级支持HDRP管线实践
    沃通SSL证书服务多省区一体化政务服务平台
    (10) 朴素贝叶斯
  • 原文地址:https://blog.csdn.net/guoguo0524/article/details/133220441