pybind播放视频

举报
风吹稻花香 发表于 2021/06/05 00:35:05 2021/06/05
【摘要】   解码挺快的,0-16ms, 但是不知道为什么,还没传数据,特别慢,400-800ms一张图片, #-*- coding:utf-8 -*-import pysdk as demoimport timefilepath="0217.h264" start=time.time()def m_callback(status,width,height): glob...

 

解码挺快的,0-16ms,

但是不知道为什么,还没传数据,特别慢,400-800ms一张图片,


  
  1. #-*- coding:utf-8 -*-
  2. import pysdk as demo
  3. import time
  4. filepath="0217.h264"
  5. start=time.time()
  6. def m_callback(status,width,height):
  7. global start
  8. print('callback ok', status,width,height,time.time()-start)
  9. start=time.time()
  10. # print('callback ok',a)
  11. filename=b'rtsp://192.168.1.1:554/h264/ch1/main/av_stream'
  12. demo.play_url(filename, m_callback)
  13. # vp= binddemo.add(128, 72, aaa)
  14. print(vp)

 

 


  
  1. int play_url(char* url, py::function callback_f)
  2. {
  3. av_log_set_level(AV_LOG_WARNING);
  4. unsigned version = avcodec_version();
  5. printf("FFmpeg version: %d\n", version);
  6. AVFormatContext *avFormatCtx = NULL;
  7. int i, videoindex;
  8. AVCodecContext *avCodecCtx = NULL;
  9. AVCodec *avCodec;
  10. avformat_network_init();
  11. //avFormatCtx = avformat_alloc_context();
  12. //if (avformat_open_input(&pFormatCtx, patha.c_str(), NULL, NULL) != 0) {
  13. if (avformat_open_input(&avFormatCtx, url, NULL, NULL) != 0) {
  14. printf("Couldn't open input stream.\n");
  15. return -1;
  16. }
  17. avCodec = NULL;
  18. while (avCodec == NULL) {
  19. printf("start find stream info \n");
  20. if (avformat_find_stream_info(avFormatCtx, NULL) < 0) {
  21. printf("Couldn't find stream info\n");
  22. goto restart_stream;
  23. continue;
  24. }
  25. videoindex = -1;
  26. for (i = 0; i < avFormatCtx->nb_streams; i++)
  27. if (avFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  28. if (videoindex == -1) {
  29. videoindex = i;
  30. }
  31. //break;
  32. }
  33. if (videoindex == -1) {
  34. printf("Didn't find a video stream.\n");
  35. goto restart_stream;
  36. }
  37. avCodecCtx = avcodec_alloc_context3(NULL);
  38. avcodec_parameters_to_context(avCodecCtx, avFormatCtx->streams[videoindex]->codecpar);
  39. avCodec = avcodec_find_decoder(avCodecCtx->codec_id);
  40. if (avCodec == NULL) {
  41. printf("Codec not found \n");
  42. goto restart_stream;
  43. //return -1;
  44. }
  45. if (avcodec_open2(avCodecCtx, avCodec, NULL) < 0) {
  46. printf("Could not open codec.\n");
  47. goto restart_stream;
  48. continue;
  49. //return -1;
  50. }
  51. goto ok;
  52. restart_stream:
  53. printf("restart 1 ");
  54. avformat_free_context(avFormatCtx);
  55. printf("restart 2 ");
  56. //avformat_close_input(&pFormatCtx);
  57. avFormatCtx = NULL;
  58. avFormatCtx = avformat_alloc_context();
  59. printf("restart 3 ");
  60. //printf("restart 4");
  61. printf("restart 4 ");
  62. int open_ret = avformat_open_input(&avFormatCtx, url, NULL, NULL);
  63. if (open_ret != 0) {
  64. printf("2Couldn't open input stream %d\n", open_ret);
  65. return -1;
  66. }
  67. avFormatCtx->probesize = 1000 * 1024;
  68. avFormatCtx->max_analyze_duration = 10 * AV_TIME_BASE;
  69. printf("restart 5\n");
  70. avCodec = NULL;
  71. continue;
  72. ok:
  73. break;
  74. }
  75. AVFrame *pFrameYUV;
  76. AVFrame *pFrameRGB;
  77. pFrameYUV = av_frame_alloc();
  78. pFrameRGB = av_frame_alloc();
  79. uint8_t *out_buffer;
  80. struct SwsContext *img_convert_ctx;
  81. img_convert_ctx = sws_getContext(avCodecCtx->width, avCodecCtx->height, avCodecCtx->pix_fmt, avCodecCtx->width, avCodecCtx->height, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
  82. out_buffer = new uint8_t[avpicture_get_size(AV_PIX_FMT_RGB24, avCodecCtx->width, avCodecCtx->height)];
  83. //avpicture_fill((AVPicture *)pFrameRGB, out_buffer, AV_PIX_FMT_RGB24, avCodecCtx->width, avCodecCtx->height);
  84. av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, out_buffer, AV_PIX_FMT_RGB24, avCodecCtx->width, avCodecCtx->height, 1);
  85. int ret, got_picture;
  86. AVPacket packet;// = (AVPacket *)av_malloc(sizeof(AVPacket));
  87. int need_decode = 1;
  88. int is_key_frame = 0;
  89. while (av_read_frame(avFormatCtx, &packet) >= 0) {
  90. if (packet.stream_index == videoindex) {
  91. DWORD start_time = GetTickCount();
  92. if (avcodec_send_packet(avCodecCtx, &packet) != 0) {
  93. printf("avcodec_send_packet错误\n");
  94. break;
  95. }
  96. while (avcodec_receive_frame(avCodecCtx, pFrameYUV) == 0) {
  97. int ret = avcodec_send_frame(avCodecCtx, pFrameYUV);
  98. pFrameYUV->pts++;
  99. printf("decode ok %d %u\n", avCodecCtx->flags, GetTickCount() - start_time);
  100. /*fwrite(pFrameYUV->data[0], (pCodecCtx->width)*(pCodecCtx->height) * 3, 1, output);*/
  101. sws_scale(img_convert_ctx, pFrameYUV->data, pFrameYUV->linesize, 0, avCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
  102. //callback_f((char*)pFrameRGB->data[0], 1, avCodecCtx->width, avCodecCtx->height);
  103. callback_f(1, avCodecCtx->width, avCodecCtx->height);
  104. avcodec_flush_buffers(avCodecCtx);
  105. //av_frame_free(&pFrameYUV);
  106. }
  107. }
  108. av_packet_unref(&packet);
  109. }
  110. avcodec_close(avCodecCtx);
  111. avformat_close_input(&avFormatCtx);
  112. return 0;
  113. }

 

 

 

 

文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/87721380

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。