16,472
社区成员
发帖
与我相关
我的任务
分享
//获取视频的长宽和总时长信息
void get_videoinfo()
{
av_register_all();
avformat_network_init();
vs = (VideoState *)av_mallocz(sizeof(VideoState));
AVFormatContext *pFormatCtx = NULL;
pFormatCtx = avformat_alloc_context();
unsigned char *aviobuffer = (unsigned char *)av_malloc(32768);
AVIOContext *avio = avio_alloc_context(aviobuffer, 32768, 0, NULL, read_buffer, NULL, NULL);
pFormatCtx->pb = avio;
pFormatCtx->flags = AVFMT_FLAG_CUSTOM_IO;
int x = avformat_open_input(&pFormatCtx, NULL, NULL, NULL);//中止播放后重新选文件也返回0
int y = avformat_find_stream_info(pFormatCtx, NULL);//中止播放后重新选文件也返回0
currentvwidth = pFormatCtx->streams[0]->codec->width;//中止播放后重新选文件变0了!!!!!!!!!!!!!!!!!
currentvheight = pFormatCtx->streams[0]->codec->height;//中止播放后重新选文件变0了!!!!!!!!!!!!!!!!!
currentvduration = (int)pFormatCtx->duration;//中止播放后重新选文件变0了!!!!!!!!!!!!!!!!!
//avcodec_close(pFormatCtx->streams[0]->codec);
avformat_close_input(&pFormatCtx);
pFormatCtx = NULL;
av_freep(vs);
vs = NULL;
avformat_network_deinit();
}
//中止播放的代码是这样的
vs->quit = true;
SDL_PauseAudio(true);
IsPlaying = false;
SDL_Event event;
event.type = FF_QUIT_EVENT;
event.user.data1 = vs;
SDL_PushEvent(&event);
SDL_WaitThread(vs->decode_tid, NULL);//等待线程停止
//播放的函数
bool StartPlay()
{
int dwNum = WideCharToMultiByte(CP_ACP, NULL, outfile, -1, NULL, 0, NULL, FALSE);
char *filepath = new char[dwNum];
if (!filepath) delete[] filepath;
WideCharToMultiByte(CP_ACP, NULL, outfile, -1, filepath, dwNum, NULL, FALSE);
av_register_all();
avformat_network_init();
if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_AUDIO))
{
avformat_network_deinit();
return true;
}
vs = (VideoState *)av_mallocz(sizeof(VideoState));
if (!vs)
{
avformat_network_deinit();
SDL_Quit();
return true;
}
av_strlcpy(vs->file, filepath, sizeof(vs->file));
vs->pictq_mutex = SDL_CreateMutex();
vs->pictq_cond = SDL_CreateCond();
video_open(vs);
schedule_refresh(vs, 100);
vs->decode_tid = SDL_CreateThread(decode_thread, "decode_thread", vs);
if (!vs->decode_tid)
{
SDL_DestroyMutex(vs->pictq_mutex);
SDL_DestroyCond(vs->pictq_cond);
av_freep(vs);
vs = NULL;
avformat_network_deinit();
SDL_Quit();
return true;
}
event_loop(vs);
return true;
}