本文中,红色字体部分是涉及到的ffmpeg部分,其他函数是视频显示部分或者内部封装函数(文中没有给出实现)。
本文的主要目的,在于记录播放视频文件时用到的ffmpeg函数,及其用法。
typedef struct _zl_player_param
{
HWND
hwnd;
//display window handle
char
filename[256];
//add for file playback
} zl_player_param;
typedef struct _zl_player
{
//ffmpeg struct
AVCodecContext
* pCodecCtx;
AVCodec
* pCodec;
struct SwsContext * pSwsCtx;
zl_video_context
* pVideoCtx;
//for video display
//add for file playback
AVFormatContext
*pFormatCtx;
int videoStream;
//视频流编号
//player state
int
abort;
//abort marker
} zl_player;
初始化播放器/
zl_player * zl_init_file_player(zl_player_param *param)
{
int ret = 0;
if (param == NULL)
{
return NULL;
}
zl_player *player = (zl_player *)malloc(sizeof(zl_player));
if (NULL == player)
{
printf("ERROR(%d): <zl_player> create player failed!\n", ERROR_MALLOC_PLAYER_SPACE);
}
memset(player, 0, sizeof(zl_player));
//register ffmpeg codec
av_register_all();
if (
avformat_open_input(&player->pFormatCtx, param->filename, NULL, NULL) != 0)//avformat_open_input
{
printf("ERROR(%d): <zl_player> avformat_open_input error!\n", ERROR_MALLOC_PLAYER_SPACE);
return NULL;
}
if (
avformat_find_stream_info(player->pFormatCtx, NULL) < 0)
{
printf("ERROR(%d): <zl_player> avformat_find_stream_info error!\n", ERROR_MALLOC_PLAYER_SPACE);
return NULL;
}
av_dump_format(player->pFormatCtx, 0, param->filename, FALSE);
//现在 pFormatCtx->streams 中已经有所有流了,因此现在我们遍历它找到第一条视频流
player->videoStream = -1;