pybind 播放h264

举报
风吹稻花香 发表于 2021/06/04 23:37:36 2021/06/04
【摘要】 python: 平均耗时在30s左右,没有cpython快。 #-*- coding:utf-8 -*-import pyiotsdk as binddemoimport timeimport numpy as npfilepath="0217.h264"import cv2start=time.time()def m_callback(a,width,height,t1...

python:

平均耗时在30s左右,没有cpython快。


  
  1. #-*- coding:utf-8 -*-
  2. import pyiotsdk as binddemo
  3. import time
  4. import numpy as np
  5. filepath="0217.h264"
  6. import cv2
  7. start=time.time()
  8. def m_callback(a,width,height,t1):
  9. global start
  10. # print(a)
  11. cv2.imshow("a",a)
  12. cv2.waitKeyEx(1)
  13. print('callback ok', width,height,time.time()-start,t1)
  14. start=time.time()
  15. return 0
  16. # print('callback ok',a)
  17. # filename=b'rtsp://admin:sbdwl123@192.168.25.45:554/h264/ch1/main/av_stream'
  18. filename=r"D:\project\hik_client_dll_4\x64\Debug\201805171600.dat"
  19. # aaa= binddemo.proxy_init3F(3.0)
  20. # aaa=aaa.astype(np.uint8)
  21. # print(aaa.shape)
  22. # # aaa=cv2.imread("d:/f1.jpg")
  23. # cv2.imshow("a",aaa)
  24. # cv2.waitKeyEx()
  25. # print(aaa)
  26. binddemo.play_url(filename, m_callback)
  27. # vp= binddemo.add(1280, 720, aaa)
  28. #
  29. # m = [[[0] * 2 for _ in range(3) ] for _ in range(4)]
  30. # print(m)

 

c++ ffmpeg 4


  
  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. AVDictionary* options = NULL;
  12. av_dict_set(&options, "buffer_size", "1024000", 0);
  13. //av_dict_set(&options, "max_delay", "500000", 0);
  14. //av_dict_set(&options, "stimeout", "20000000", 0); //设置超时断开连接时间
  15. av_dict_set(&options, "rtsp_transport", "udp", 0); //以udp方式打开,如果以tcp方式打开将udp替换为tcp
  16. if (avformat_open_input(&avFormatCtx, url, NULL, &options) != 0) {
  17. //if (avformat_open_input(&avFormatCtx, url, NULL, NULL) != 0) {
  18. printf("Couldn't open input stream.\n");
  19. return -1;
  20. }
  21. avCodec = NULL;
  22. while (avCodec == NULL) {
  23. printf("start find stream info \n");
  24. if (avformat_find_stream_info(avFormatCtx, NULL) < 0) {
  25. printf("Couldn't find stream info\n");
  26. goto restart_stream;
  27. continue;
  28. }
  29. videoindex = -1;
  30. for (i = 0; i < avFormatCtx->nb_streams; i++)
  31. if (avFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  32. if (videoindex == -1) {
  33. videoindex = i;
  34. }
  35. //break;
  36. }
  37. if (videoindex == -1) {
  38. printf("Didn't find a video stream.\n");
  39. goto restart_stream;
  40. }
  41. avCodecCtx = avcodec_alloc_context3(NULL);
  42. avcodec_parameters_to_context(avCodecCtx, avFormatCtx->streams[videoindex]->codecpar);
  43. av_opt_set(avCodecCtx->priv_data, "preset", "ultrafast", 0);
  44. av_opt_set(avCodecCtx->priv_data, "tune", "zerolatency", 0); //
  45. avCodec = avcodec_find_decoder(avCodecCtx->codec_id);
  46. if (avCodec == NULL) {
  47. printf("Codec not found \n");
  48. goto restart_stream;
  49. //return -1;
  50. }
  51. if (avcodec_open2(avCodecCtx, avCodec, NULL) < 0) {
  52. printf("Could not open codec.\n");
  53. goto restart_stream;
  54. continue;
  55. //return -1;
  56. }
  57. goto ok;
  58. restart_stream:
  59. printf("restart 1 ");
  60. avformat_free_context(avFormatCtx);
  61. printf("restart 2 ");
  62. //avformat_close_input(&pFormatCtx);
  63. avFormatCtx = NULL;
  64. avFormatCtx = avformat_alloc_context();
  65. printf("restart 3 ");
  66. //printf("restart 4");
  67. printf("restart 4 ");
  68. int open_ret = avformat_open_input(&avFormatCtx, url, NULL, NULL);
  69. if (open_ret != 0) {
  70. printf("2Couldn't open input stream %d\n", open_ret);
  71. return -1;
  72. }
  73. avFormatCtx->probesize = 1000 * 1024;
  74. avFormatCtx->max_analyze_duration = 10 * AV_TIME_BASE;
  75. printf("restart 5\n");
  76. avCodec = NULL;
  77. continue;
  78. ok:
  79. break;
  80. }
  81. AVFrame *pFrameYUV;
  82. AVFrame *pFrameRGB;
  83. pFrameYUV = av_frame_alloc();
  84. pFrameRGB = av_frame_alloc();
  85. uint8_t *out_buffer;
  86. struct SwsContext *img_convert_ctx;
  87. 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);
  88. out_buffer = new uint8_t[avpicture_get_size(AV_PIX_FMT_RGB24, avCodecCtx->width, avCodecCtx->height)];
  89. //avpicture_fill((AVPicture *)pFrameRGB, out_buffer, AV_PIX_FMT_RGB24, avCodecCtx->width, avCodecCtx->height);
  90. av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, out_buffer, AV_PIX_FMT_RGB24, avCodecCtx->width, avCodecCtx->height, 1);
  91. AVPacket packet;// = (AVPacket *)av_malloc(sizeof(AVPacket));
  92. int need_decode = 1;
  93. int is_key_frame = 0;
  94. DWORD start_time = GetTickCount();
  95. while (av_read_frame(avFormatCtx, &packet) >= 0) {
  96. if (packet.stream_index == videoindex) {
  97. if (avcodec_send_packet(avCodecCtx, &packet) != 0) {
  98. printf("avcodec_send_packet错误\n");
  99. break;
  100. }
  101. while (avcodec_receive_frame(avCodecCtx, pFrameYUV) == 0) {
  102. printf("decode ok %d %u\n", avCodecCtx->flags, GetTickCount() - start_time);
  103. start_time = GetTickCount();
  104. sws_scale(img_convert_ctx, pFrameYUV->data, pFrameYUV->linesize, 0, avCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
  105. /*fwrite(pFrameYUV->data[0], (pCodecCtx->width)*(pCodecCtx->height) * 3, 1, output);*/
  106. py::array_t<uint8_t> result(avCodecCtx->height *avCodecCtx->width* 3 );//h w c
  107. //std:s:fill(result.mutable_data(), vptr, 720 * 1280 * 3);
  108. memcpy(result.mutable_data(), pFrameRGB->data[0], avCodecCtx->height *avCodecCtx->width * 3);
  109. //callback_f(data_rgb, 1, avCodecCtx->width, avCodecCtx->height, GetTickCount());
  110. callback_f(result, avCodecCtx->width, avCodecCtx->height, GetTickCount());
  111. //av_frame_free(&pFrameYUV);
  112. }
  113. }
  114. av_packet_unref(&packet);
  115. }
  116. avcodec_close(avCodecCtx);
  117. avformat_close_input(&avFormatCtx);
  118. return 0;
  119. }

 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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