FFmpeg开发实战(五):FFmpeg 抽取音视频的视频数据

时间:2023-03-10 06:37:19
FFmpeg开发实战(五):FFmpeg 抽取音视频的视频数据

如何使用FFmpeg抽取音视频的视频数据,代码如下:

// FFmpegTest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// #include "pch.h"
#include <iostream>
#include "AACFormat.h" #define __STDC_CONSTANT_MACROS
#define __STDC_FORMAT_MACROS // For extractVideo
#ifndef AV_WB32
# define AV_WB32(p, val) do { \
uint32_t d = (val); \
((uint8_t*)(p))[] = (d); \
((uint8_t*)(p))[] = (d)>>; \
((uint8_t*)(p))[] = (d)>>; \
((uint8_t*)(p))[] = (d)>>; \
} while()
#endif // For extractVideo
#ifndef AV_RB16
# define AV_RB16(x) \
((((const uint8_t*)(x))[] << ) | \
((const uint8_t*)(x))[])
#endif extern "C"{
#include <stdio.h>
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/log.h"
} // For extractVideo
static int alloc_and_copy(AVPacket *out,
const uint8_t *sps_pps, uint32_t sps_pps_size,
const uint8_t *in, uint32_t in_size)
{
uint32_t offset = out->size;
uint8_t nal_header_size = offset ? : ;
int err; err = av_grow_packet(out, sps_pps_size + in_size + nal_header_size);
if (err < )
return err; if (sps_pps)
memcpy(out->data + offset, sps_pps, sps_pps_size);
memcpy(out->data + sps_pps_size + nal_header_size + offset, in, in_size);
if (!offset) {
AV_WB32(out->data + sps_pps_size, );
}
else {
(out->data + offset + sps_pps_size)[] =
(out->data + offset + sps_pps_size)[] = ;
(out->data + offset + sps_pps_size)[] = ;
} return ;
} // For extractVideo
int h264_extradata_to_annexb(const uint8_t *codec_extradata, const int codec_extradata_size, AVPacket *out_extradata, int padding)
{
uint16_t unit_size;
uint64_t total_size = ;
uint8_t *out = NULL, unit_nb, sps_done = ,
sps_seen = , pps_seen = , sps_offset = , pps_offset = ;
const uint8_t *extradata = codec_extradata + ;
static const uint8_t nalu_header[] = { , , , };
int length_size = (*extradata++ & 0x3) + ; // retrieve length coded size, 用于指示表示编码数据长度所需字节数 sps_offset = pps_offset = -; /* retrieve sps and pps unit(s) */
unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
if (!unit_nb) {
goto pps;
}
else {
sps_offset = ;
sps_seen = ;
} while (unit_nb--) {
int err; unit_size = AV_RB16(extradata);
total_size += unit_size + ;
if (total_size > INT_MAX - padding) {
av_log(NULL, AV_LOG_ERROR,
"Too big extradata size, corrupted stream or invalid MP4/AVCC bitstream\n");
av_free(out);
return AVERROR(EINVAL);
}
if (extradata + + unit_size > codec_extradata + codec_extradata_size) {
av_log(NULL, AV_LOG_ERROR, "Packet header is not contained in global extradata, "
"corrupted stream or invalid MP4/AVCC bitstream\n");
av_free(out);
return AVERROR(EINVAL);
}
if ((err = av_reallocp(&out, total_size + padding)) < )
return err;
memcpy(out + total_size - unit_size - , nalu_header, );
memcpy(out + total_size - unit_size, extradata + , unit_size);
extradata += + unit_size;
pps:
if (!unit_nb && !sps_done++) {
unit_nb = *extradata++; /* number of pps unit(s) */
if (unit_nb) {
pps_offset = total_size;
pps_seen = ;
}
}
} if (out)
memset(out + total_size, , padding); if (!sps_seen)
av_log(NULL, AV_LOG_WARNING,
"Warning: SPS NALU missing or invalid. "
"The resulting stream may not play.\n"); if (!pps_seen)
av_log(NULL, AV_LOG_WARNING,
"Warning: PPS NALU missing or invalid. "
"The resulting stream may not play.\n"); out_extradata->data = out;
out_extradata->size = total_size; return length_size;
} // For extractVideo
int h264_mp4toannexb(AVFormatContext *fmt_ctx, AVPacket *in, FILE *dst_fd)
{ AVPacket *out = NULL;
AVPacket spspps_pkt; int len;
uint8_t unit_type;
int32_t nal_size;
uint32_t cumul_size = ;
const uint8_t *buf;
const uint8_t *buf_end;
int buf_size;
int ret = , i; out = av_packet_alloc(); buf = in->data;
buf_size = in->size;
buf_end = in->data + in->size; do {
ret = AVERROR(EINVAL);
if (buf + /*s->length_size*/ > buf_end)
goto fail; for (nal_size = , i = ; i < /*s->length_size*/; i++)
nal_size = (nal_size << ) | buf[i]; buf += ; /*s->length_size;*/
unit_type = *buf & 0x1f; if (nal_size > buf_end - buf || nal_size < )
goto fail; /*
if (unit_type == 7)
s->idr_sps_seen = s->new_idr = 1;
else if (unit_type == 8) {
s->idr_pps_seen = s->new_idr = 1;
*/
/* if SPS has not been seen yet, prepend the AVCC one to PPS */
/*
if (!s->idr_sps_seen) {
if (s->sps_offset == -1)
av_log(ctx, AV_LOG_WARNING, "SPS not present in the stream, nor in AVCC, stream may be unreadable\n");
else {
if ((ret = alloc_and_copy(out,
ctx->par_out->extradata + s->sps_offset,
s->pps_offset != -1 ? s->pps_offset : ctx->par_out->extradata_size - s->sps_offset,
buf, nal_size)) < 0)
goto fail;
s->idr_sps_seen = 1;
goto next_nal;
}
}
}
*/ /* if this is a new IDR picture following an IDR picture, reset the idr flag.
* Just check first_mb_in_slice to be 0 as this is the simplest solution.
* This could be checking idr_pic_id instead, but would complexify the parsing. */
/*
if (!s->new_idr && unit_type == 5 && (buf[1] & 0x80))
s->new_idr = 1; */
/* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */
if (/*s->new_idr && */unit_type == /*&& !s->idr_sps_seen && !s->idr_pps_seen*/) { h264_extradata_to_annexb(fmt_ctx->streams[in->stream_index]->codec->extradata,
fmt_ctx->streams[in->stream_index]->codec->extradata_size,
&spspps_pkt,
AV_INPUT_BUFFER_PADDING_SIZE); if ((ret = alloc_and_copy(out,
spspps_pkt.data, spspps_pkt.size,
buf, nal_size)) < )
goto fail;
/*s->new_idr = 0;*/
/* if only SPS has been seen, also insert PPS */
}
/*else if (s->new_idr && unit_type == 5 && s->idr_sps_seen && !s->idr_pps_seen) {
if (s->pps_offset == -1) {
av_log(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n");
if ((ret = alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0)
goto fail;
} else if ((ret = alloc_and_copy(out,
ctx->par_out->extradata + s->pps_offset, ctx->par_out->extradata_size - s->pps_offset,
buf, nal_size)) < 0)
goto fail;
}*/ else {
if ((ret = alloc_and_copy(out, NULL, , buf, nal_size)) < )
goto fail;
/*
if (!s->new_idr && unit_type == 1) {
s->new_idr = 1;
s->idr_sps_seen = 0;
s->idr_pps_seen = 0;
}
*/
} len = fwrite(out->data, , out->size, dst_fd);
if (len != out->size) {
av_log(NULL, AV_LOG_DEBUG, "warning, length of writed data isn't equal pkt.size(%d, %d)\n",
len,
out->size);
}
fflush(dst_fd); next_nal:
buf += nal_size;
cumul_size += nal_size + ;//s->length_size;
} while (cumul_size < buf_size); /*
ret = av_packet_copy_props(out, in);
if (ret < 0)
goto fail; */
fail:
av_packet_free(&out); return ret;
} // 使用FFmpeg从视频中抽取视频
void extractVideo() { // Start Code 特征码
// PSP PPS(从codec->extradata即在数据空间中存放) // 设置日志输出等级
av_log_set_level(AV_LOG_INFO); AVFormatContext *fmt_ctx = NULL;
AVPacket pkt; // 注册所有的格式和编解码器
av_register_all(); int ret;
int len;
int video_index = -; // 打开输入文件
ret = avformat_open_input(&fmt_ctx, "111.mp4", NULL, NULL); // 检查打开输入文件是否成功
if (ret < ) {
printf("cant open file,error message = %s", av_err2str(ret));
return;
}
   // 打开输出文件
FILE* dst_fd = fopen("111.H264", "wb"); // w 写入 b 二进制文件 // 检查输出文件打开是否成功,如果失败,就输出日志,并关闭输出文件的引用
if (!dst_fd) {
av_log(NULL, AV_LOG_ERROR, "Can't Open Out File!\n");
avformat_close_input(&fmt_ctx);
} av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = ; for (int i = ; i < fmt_ctx->nb_streams; i++) {
if (fmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
video_index = i;
break;
}
} printf("Video Stream Index = %d", video_index); // 检查发现视频流的结果
if (video_index < ) {
av_log(NULL, AV_LOG_ERROR, "Can't find Best Video Stream!\n");
//printf("Reason = %s", av_err2str(ret));
// 关闭输出文件和输出文件的引用
avformat_close_input(&fmt_ctx);
fclose(dst_fd);
return;
} while (av_read_frame(fmt_ctx, &pkt) >= ) {
if (pkt.stream_index == video_index) {
h264_mp4toannexb(fmt_ctx, &pkt, dst_fd);
}
// 将引用基数减一
av_packet_unref(&pkt);
} // 关闭文件(输入/输出)
avformat_close_input(&fmt_ctx);
if (dst_fd) {
fclose(dst_fd);
}
} int main(int argc, char* argv[]) {
/**使用FFmpeg从视频中抽取视频 **/
extractVideo();
return ;
}