一、ffmpeg获取视频时长代码
AVFormatContext *pFormatCtx = NULL;
if (avformat_open_input(&pFormatCtx, filename, NULL, NULL) != 0) {
// Failed to open file or unknown error occurred
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
// Failed to retrieve stream information
}
int duration = pFormatCtx->duration / AV_TIME_BASE;
这段代码展示了如何使用ffmpeg在C++中获取视频文件的时长。首先,打开文件并获得AVFormatContext
(格式上下文)结构体的指针。接着使用avformat_find_stream_info
函数来获取文件的媒体流信息。最后通过duration
字段获取文件时长,单位是毫秒,需要除以AV_TIME_BASE
才能得到秒数。
二、ffmpeg获取视频码率
AVFormatContext *pFormatCtx = NULL;
if (avformat_open_input(&pFormatCtx, filename, NULL, NULL) != 0) {
// Failed to open file or unknown error occurred
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
// Failed to retrieve stream information
}
int bitrate = pFormatCtx->bit_rate;
这段代码展示了如何使用ffmpeg在C++中获取视频文件的码率。描述同上,但是码率单位是比特每秒。
三、ffmpeg获取视频时长为负数
在某些情况下,pFormatCtx->duration
的值可以是负数。这种情况发生在视频文件不包含时长信息时,ffmpeg无法确定媒体流中的最后一帧在文件中的时间戳。
if (pFormatCtx->duration <= 0) {
int64_t last_dts = av_rescale_q(pFormatCtx->streams[0]->nb_frames - 1, pFormatCtx->streams[0]->time_base, AV_TIME_BASE_Q);
int64_t last_pts = av_rescale_q(pFormatCtx->streams[0]->nb_frames - 1, pFormatCtx->streams[0]->time_base, AV_TIME_BASE_Q);
if (last_dts > last_pts) {
duration = (int)(last_dts / AV_TIME_BASE);
} else {
duration = (int)(last_pts / AV_TIME_BASE);
}
}
这段代码展示了如果duration
小于等于0时,计算文件时长的方法。首先,计算最后一帧的时间戳(即dts
和pts
中较大的那个)。最后将时间戳除以AV_TIME_BASE
得到秒数。
四、ffmpeg获取视频时长帧数原理
在做视频处理时,有时候需要知道视频的帧数以计算处理时间,以下是ffmpeg计算帧数的原理。
AVFormatContext *pFormatCtx = NULL;
if (avformat_open_input(&pFormatCtx, filename, NULL, NULL) != 0) {
// Failed to open file or unknown error occurred
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
// Failed to retrieve stream information
}
AVStream *videoStream = NULL;
for (int i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = pFormatCtx->streams[i];
break;
}
}
int frameCount = (int)(videoStream->duration * videoStream->avg_frame_rate.num / videoStream->avg_frame_rate.den);
这段代码展示了如何使用ffmpeg在C++中获取视频文件帧数。首先,获取视频流。接着计算帧数,计算公式为:duration * avg_frame_rate.num / avg_frame_rate.den
。
五、ffmpeg获取视频信息
AVFormatContext *pFormatCtx = NULL;
if (avformat_open_input(&pFormatCtx, filename, NULL, NULL) != 0) {
// Failed to open file or unknown error occurred
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
// Failed to retrieve stream information
}
av_dump_format(pFormatCtx, 0, filename, 0);
这段代码展示了如何使用ffmpeg在C++中获取视频文件的基本信息。调用av_dump_format
函数输出视频的基本信息,如格式、时长、码率、帧率等信息。
六、ffmpeg获取视频帧率
AVFormatContext *pFormatCtx = NULL;
if (avformat_open_input(&pFormatCtx, filename, NULL, NULL) != 0) {
// Failed to open file or unknown error occurred
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
// Failed to retrieve stream information
}
AVStream *videoStream = NULL;
for (int i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = pFormatCtx->streams[i];
break;
}
}
float frameRate = av_q2d(videoStream->r_frame_rate);
这段代码展示了如何使用ffmpeg在C++中获取视频文件的帧率。首先,获取视频流。接着使用av_q2d
函数将AVRational
结构体转换为浮点数,得到帧率。
七、ffmpeg获取视频长度
AVFormatContext *pFormatCtx = NULL;
if (avformat_open_input(&pFormatCtx, filename, NULL, NULL) != 0) {
// Failed to open file or unknown error occurred
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
// Failed to retrieve stream information
}
int64_t duration = pFormatCtx->streams[0]->duration;
这段代码展示了如何使用ffmpeg在C++中获取视频文件的长度。获取第一个媒体流的duration
字段,可以得到视频文件长度,单位是毫秒。
八、ffmpeg 视频时长
以下是使用ffmpeg命令行获取视频文件时长的方法:
ffmpeg -i filename -vcodec copy -acodec copy -f null /dev/null
输出示例:
duration: 00:00:05.01, bitrate: 556 kb/s
九、ffmpeg 获取视频流
以下是使用ffmpeg命令行获取视频文件数据流的方法:
ffmpeg -i filename -c copy output.mp4
输出示例:
Stream #0:0(und): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 480x270, 307 kb/s, 25 fps, 25 tbr, 90k tbn, 180k tbc (default)
总结
本文详细介绍了ffmpeg获取视频时长的多个方面,包括获取视频时长代码、获取视频码率、获取视频帧数、获取视频信息、获取视频帧率和获取视频长度等。此外,还介绍了在一些情况下,ffmpeg获取到的视频时长可能为负数的解决方法,以及使用ffmpeg命令行获取视频时长和视频数据流的方法。