pybind播放视频
【摘要】
解码挺快的,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一张图片,
-
#-*- coding:utf-8 -*-
-
import pysdk as demo
-
import time
-
filepath="0217.h264"
-
-
start=time.time()
-
def m_callback(status,width,height):
-
global start
-
print('callback ok', status,width,height,time.time()-start)
-
start=time.time()
-
# print('callback ok',a)
-
-
-
filename=b'rtsp://192.168.1.1:554/h264/ch1/main/av_stream'
-
-
demo.play_url(filename, m_callback)
-
# vp= binddemo.add(128, 72, aaa)
-
print(vp)
-
int play_url(char* url, py::function callback_f)
-
{
-
-
av_log_set_level(AV_LOG_WARNING);
-
unsigned version = avcodec_version();
-
-
printf("FFmpeg version: %d\n", version);
-
-
AVFormatContext *avFormatCtx = NULL;
-
int i, videoindex;
-
AVCodecContext *avCodecCtx = NULL;
-
AVCodec *avCodec;
-
avformat_network_init();
-
//avFormatCtx = avformat_alloc_context();
-
//if (avformat_open_input(&pFormatCtx, patha.c_str(), NULL, NULL) != 0) {
-
if (avformat_open_input(&avFormatCtx, url, NULL, NULL) != 0) {
-
printf("Couldn't open input stream.\n");
-
return -1;
-
}
-
avCodec = NULL;
-
while (avCodec == NULL) {
-
printf("start find stream info \n");
-
if (avformat_find_stream_info(avFormatCtx, NULL) < 0) {
-
printf("Couldn't find stream info\n");
-
goto restart_stream;
-
continue;
-
}
-
videoindex = -1;
-
for (i = 0; i < avFormatCtx->nb_streams; i++)
-
if (avFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
-
if (videoindex == -1) {
-
videoindex = i;
-
}
-
//break;
-
}
-
if (videoindex == -1) {
-
printf("Didn't find a video stream.\n");
-
goto restart_stream;
-
}
-
avCodecCtx = avcodec_alloc_context3(NULL);
-
avcodec_parameters_to_context(avCodecCtx, avFormatCtx->streams[videoindex]->codecpar);
-
avCodec = avcodec_find_decoder(avCodecCtx->codec_id);
-
if (avCodec == NULL) {
-
printf("Codec not found \n");
-
goto restart_stream;
-
//return -1;
-
}
-
if (avcodec_open2(avCodecCtx, avCodec, NULL) < 0) {
-
printf("Could not open codec.\n");
-
goto restart_stream;
-
continue;
-
//return -1;
-
}
-
goto ok;
-
restart_stream:
-
printf("restart 1 ");
-
avformat_free_context(avFormatCtx);
-
printf("restart 2 ");
-
//avformat_close_input(&pFormatCtx);
-
avFormatCtx = NULL;
-
avFormatCtx = avformat_alloc_context();
-
printf("restart 3 ");
-
//printf("restart 4");
-
printf("restart 4 ");
-
-
int open_ret = avformat_open_input(&avFormatCtx, url, NULL, NULL);
-
if (open_ret != 0) {
-
printf("2Couldn't open input stream %d\n", open_ret);
-
return -1;
-
}
-
avFormatCtx->probesize = 1000 * 1024;
-
avFormatCtx->max_analyze_duration = 10 * AV_TIME_BASE;
-
printf("restart 5\n");
-
avCodec = NULL;
-
continue;
-
ok:
-
break;
-
}
-
-
AVFrame *pFrameYUV;
-
AVFrame *pFrameRGB;
-
pFrameYUV = av_frame_alloc();
-
pFrameRGB = av_frame_alloc();
-
-
-
uint8_t *out_buffer;
-
-
struct SwsContext *img_convert_ctx;
-
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);
-
-
out_buffer = new uint8_t[avpicture_get_size(AV_PIX_FMT_RGB24, avCodecCtx->width, avCodecCtx->height)];
-
//avpicture_fill((AVPicture *)pFrameRGB, out_buffer, AV_PIX_FMT_RGB24, avCodecCtx->width, avCodecCtx->height);
-
av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, out_buffer, AV_PIX_FMT_RGB24, avCodecCtx->width, avCodecCtx->height, 1);
-
-
int ret, got_picture;
-
-
AVPacket packet;// = (AVPacket *)av_malloc(sizeof(AVPacket));
-
-
int need_decode = 1;
-
int is_key_frame = 0;
-
-
while (av_read_frame(avFormatCtx, &packet) >= 0) {
-
if (packet.stream_index == videoindex) {
-
DWORD start_time = GetTickCount();
-
if (avcodec_send_packet(avCodecCtx, &packet) != 0) {
-
printf("avcodec_send_packet错误\n");
-
break;
-
}
-
while (avcodec_receive_frame(avCodecCtx, pFrameYUV) == 0) {
-
-
int ret = avcodec_send_frame(avCodecCtx, pFrameYUV);
-
pFrameYUV->pts++;
-
printf("decode ok %d %u\n", avCodecCtx->flags, GetTickCount() - start_time);
-
/*fwrite(pFrameYUV->data[0], (pCodecCtx->width)*(pCodecCtx->height) * 3, 1, output);*/
-
sws_scale(img_convert_ctx, pFrameYUV->data, pFrameYUV->linesize, 0, avCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
-
-
//callback_f((char*)pFrameRGB->data[0], 1, avCodecCtx->width, avCodecCtx->height);
-
callback_f(1, avCodecCtx->width, avCodecCtx->height);
-
avcodec_flush_buffers(avCodecCtx);
-
//av_frame_free(&pFrameYUV);
-
-
}
-
}
-
av_packet_unref(&packet);
-
}
-
-
-
avcodec_close(avCodecCtx);
-
avformat_close_input(&avFormatCtx);
-
return 0;
-
}
文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/87721380
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)