1. Log
#define _CRT_SECURE_NO_WARNINGS
extern "C"
{
#include <libavformat/avformat.h>
}
int main()
{
av_log_set_level(AV_LOG_INFO);
av_log(NULL, AV_LOG_PANIC, "1.Hello FFMPEG!\n");
av_log(NULL, AV_LOG_FATAL, "2.Hello FFMPEG!\n");
av_log(NULL, AV_LOG_ERROR, "3.Hello FFMPEG!\n");
av_log(NULL, AV_LOG_WARNING, "4.Hello FFMPEG!\n");
av_log(NULL, AV_LOG_INFO, "5.Hello FFMPEG!\n");
av_log(NULL, AV_LOG_VERBOSE, "6.Hello FFMPEG!\n");
av_log(NULL, AV_LOG_DEBUG, "7.Hello FFMPEG!\n");
av_log(NULL, AV_LOG_TRACE, "8.Hello FFMPEG!\n");
}
输出:
2. Media Info
AVFormatContext
av_register_all
avformat_open_input
av_dump_forma
avformat_close_input
#define _CRT_SECURE_NO_WARNINGS
extern "C"
{
#include <libavformat/avformat.h>
}
int main()
{
av_log_set_level(AV_LOG_INFO);
av_register_all();
AVFormatContext *fmt_ctx = NULL;
//打开多媒体文件
int ret = avformat_open_input(&fmt_ctx, "./1.mp4", NULL, NULL);
if (ret < 0)
{
av_log(NULL, AV_LOG_INFO, "avformat_open_input fail!\n");
return -1;
}
/**
* Print detailed information about the input or output format, such as
* duration, bitrate, streams, container, programs, metadata, side data,
* codec and time base.
*
* @param ic the context to analyze
* @param index index of the stream to dump information about
* @param url the URL to print, such as source or destination file
* @param is_output Select whether the specified context is an input(0) or output(1)
*/
av_dump_format(fmt_ctx, 0, "./1.mp4", 0);
avformat_close_input(&fmt_ctx);
}
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from './1.mp4':
Metadata:
major_brand : isom
minor_version : 0
compatible_brands: isommp42
encoder : Lavf57.56.100
Duration: 00:13:49.35, bitrate: N/A
Stream #0:0(eng): Video: h264 (avc1 / 0x31637661), none, 1920x1080, 283 kb/s, 18 fps, 18 tbr, 90k tbn (default)
Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 44100 Hz, 2 channels, 129 kb/s (default)
3.从文件中提取音频数据
#define _CRT_SECURE_NO_WARNINGS
extern "C"
{
#include <libavformat/avformat.h>
}
#define ADTS_HEADER_LEN 7;
void adts_header_v1(char *szAdtsHeader, int dataLen){
int audio_object_type = 2;
int sampling_frequency_index = 4; //4: 44100 Hz
int channel_config = 2;
int adtsLen = dataLen + 7;
szAdtsHeader[0] = 0xff; //syncword:0xfff 高8bits
szAdtsHeader[1] = 0xf0; //syncword:0xfff 低4bits
szAdtsHeader[1] |= (0 << 3); //MPEG Version:0 for MPEG-4,1 for MPEG-2 1bit
szAdtsHeader[1] |= (0 << 1); //Layer:0 2bits
szAdtsHeader[1] |= 1; //protection absent:1 1bit
szAdtsHeader[2] = (audio_object_type - 1) << 6; //profile:audio_object_type - 1 2bits
szAdtsHeader[2] |= (sampling_frequency_index & 0x0f) << 2; //sampling frequency index:sampling_frequency_index 4bits
szAdtsHeader[2] |= (0 << 1); //private bit:0 1bit
szAdtsHeader[2] |= (channel_config & 0x04) >> 2; //channel configuration:channel_config 高1bit
szAdtsHeader[3] = (channel_config & 0x03) << 6; //channel configuration:channel_config 低2bits
szAdtsHeader[3] |= (0 << 5); //original:0 1bit
szAdtsHeader[3] |= (0 << 4); //home:0 1bit
szAdtsHeader[3] |= (0 << 3); //copyright id bit:0 1bit
szAdtsHeader[3] |= (0 << 2); //copyright id start:0 1bit
szAdtsHeader[3] |= ((adtsLen & 0x1800) >> 11); //frame length:value 高2bits
szAdtsHeader[4] = (uint8_t)((adtsLen & 0x7f8) >> 3); //frame length:value 中间8bits
szAdtsHeader[5] = (uint8_t)((adtsLen & 0x7) << 5); //frame length:value 低3bits
szAdtsHeader[5] |= 0x1f; //buffer fullness:0x7ff 高5bits
szAdtsHeader[6] = 0xfc;
}
int main()
{
av_log_set_level(AV_LOG_INFO);
av_register_all();
AVFormatContext *fmt_ctx = NULL;
//打开多媒体文件
int ret = avformat_open_input(&fmt_ctx, "./1.mp4", NULL, NULL);
if (ret < 0)
{
av_log(NULL, AV_LOG_INFO, "avformat_open_input fail!\n");
return -1;
}
av_dump_format(fmt_ctx, 0, "./1.mp4", 0);
FILE *file = fopen("./test.aac", "wb");
if (file == NULL)
{
av_log(NULL, AV_LOG_INFO, "fopen fail!\n");
goto FMT_ERROR;
}
ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
if (ret < 0)
{
av_log(NULL, AV_LOG_INFO, "av_find_best_stream fail!\n");
goto FMT_ERROR;
}
int audio_index = ret;
AVPacket pkt;
av_init_packet(&pkt);
int index = -1;
while ((index = av_read_frame(fmt_ctx, &pkt)) >= 0)
{
if (pkt.stream_index == audio_index)
{
char adts_header_buf[7];
adts_header_v1(adts_header_buf, pkt.size);
fwrite(adts_header_buf, 1, 7, file);
int n = fwrite(pkt.data, 1, pkt.size, file);
if (n != pkt.size)
{
av_log(NULL, AV_LOG_ERROR, "fwrite fail!\n");
goto FMT_ERROR;
}
}
av_packet_unref(&pkt);
}
FMT_ERROR:
avformat_close_input(&fmt_ctx);
fclose(file);
}
4.从文件中提取视频流h264
#define _CRT_SECURE_NO_WARNINGS
extern "C"
{
#include <libavformat/avformat.h>
}
#ifndef AV_WB32
# define AV_WB32(p, val) do { \
uint32_t d = (val); \
((uint8_t*)(p))[3] = (d); \
((uint8_t*)(p))[2] = (d) >> 8; \
((uint8_t*)(p))[1] = (d) >> 16; \
((uint8_t*)(p))[0] = (d) >> 24; \
} while (0)
#endif
#ifndef AV_RB16
# define AV_RB16(x) \
((((const uint8_t*)(x))[0] << 8) | \
((const uint8_t*)(x))[1])
#endif
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 ? 3 : 4;
int err;
err = av_grow_packet(out, sps_pps_size + in_size + nal_header_size);
if (err < 0)
return err;
//copy pps/sps
if (sps_pps)
{
memcpy(out->data + offset, sps_pps, sps_pps_size);
}
//copy data
memcpy(out->data + sps_pps_size + nal_header_size + offset, in, in_size);
//copy start code
if (!offset) {
AV_WB32(out->data + sps_pps_size, 1);
}
else
{
(out->data + offset + sps_pps_size)[0] = 0;
(out->data + offset + sps_pps_size)[1] = 0;
(out->data + offset + sps_pps_size)[2] = 1;
}
return 0;
}
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 = 0;
uint8_t *out = NULL, unit_nb, sps_done = 0,
sps_seen = 0, pps_seen = 0, sps_offset = 0, pps_offset = 0;
const uint8_t *extradata = codec_extradata + 4;//扩展数据前4个字节是无用的,需要跳过
static const uint8_t nalu_header[4] = { 0, 0, 0, 1 };
int length_size = (*extradata++ & 0x3) + 1; // retrieve length coded size, 用于指示表示编码数据长度所需字节数
sps_offset = pps_offset = -1;
/* retrieve sps and pps unit(s) */
unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
if (!unit_nb) {
goto pps;
}
else {
sps_offset = 0;
sps_seen = 1;
}
while (unit_nb--) {
int err;
unit_size = AV_RB16(extradata);
total_size += unit_size + 4;
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 + 2 + 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)) < 0)
return err;
memcpy(out + total_size - unit_size - 4, nalu_header, 4);
memcpy(out + total_size - unit_size, extradata + 2, unit_size);
extradata += 2 + 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 = 1;
}
}
}
if (out)
memset(out + total_size, 0, 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;
}
int h264_mp4toannexb(AVFormatContext *fmt_ctx, AVPacket *in, FILE *dst_fd)
{
AVPacket spspps_pkt;
uint32_t cumul_size = 0;
AVPacket *out = av_packet_alloc();
const uint8_t *buf = in->data;
int buf_size = in->size;
const uint8_t *buf_end = in->data + in->size;
int ret = -1;
do {
ret = AVERROR(EINVAL);
if (buf + 4 /*s->length_size*/ > buf_end)
goto fail;
int32_t nal_size = 0;
//大端模式---》低字节在高地址,高字节在低地址,0x12345678,存放的buf[0]->12, buf[1]->34, buf[2]->56, buf[1]->78
for (int i = 0; i < 4; i++)
nal_size = (nal_size << 8) | buf[i];
buf += 4; /*s->length_size;*/
//一帧数据的第一个字节后五位是这一帧的类型sps pps
uint8_t unit_type = *buf & 0x1f;
if (nal_size > buf_end - buf || nal_size < 0)
goto fail;
//IDR frame
if (unit_type == 5)
{
//关键帧需要添加sps pps
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)) < 0)
goto fail;
}
else
{
//非关键帧不需要添加sps pps
if ((ret = alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0)
goto fail;
}
int len = fwrite(out->data, 1, 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 + 4;//s->length_size;
}
while (cumul_size < buf_size);
fail:
av_packet_free(&out);
return ret;
}
int main()
{
av_log_set_level(AV_LOG_INFO);
av_register_all();
AVFormatContext *fmt_ctx = NULL;
//打开多媒体文件
int ret = avformat_open_input(&fmt_ctx, "./1.mp4", NULL, NULL);
if (ret < 0)
{
av_log(NULL, AV_LOG_INFO, "avformat_open_input fail!\n");
return -1;
}
av_dump_format(fmt_ctx, 0, "./1.mp4", 0);
FILE *file = fopen("./test.h264", "wb");
if (file == NULL)
{
av_log(NULL, AV_LOG_INFO, "fopen fail!\n");
goto FMT_ERROR;
}
ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if (ret < 0)
{
av_log(NULL, AV_LOG_INFO, "av_find_best_stream fail!\n");
goto FMT_ERROR;
}
int video_index = ret;
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
int index = -1;
while ((index = av_read_frame(fmt_ctx, &pkt)) >= 0)
{
if (pkt.stream_index == video_index)
{
h264_mp4toannexb(fmt_ctx, &pkt, file);
}
av_packet_unref(&pkt);
}
FMT_ERROR:
avformat_close_input(&fmt_ctx);
fclose(file);
}
5.MP4转FLV, remuxing
#define __STDC_CONSTANT_MACROS
#include <stdio.h>
extern "C"
{
#include <libavutil/avutil.h>
#include <libavformat/avformat.h>
}
/*
mp4转FLV格式
1. avformat_write_header(); 生成多媒体文件头
2. av_write_frame(); av_interleaved_write_frame(); 生成多媒体文件的数据
3. av_write_trailer();生成多媒体文件的尾部数据
*/
int main(int argc, char **argv)
{
av_log_set_level(AV_LOG_DEBUG);
AVOutputFormat *ofmt = NULL;
const char *in_filename, *out_filename;
int ret, i;
int stream_index = 0;
int stream_mapping_size = 0;
if (argc < 3) {
printf("usage: %s input output\n"
"API example program to remux a media file with libavformat and libavcodec.\n"
"The output format is guessed according to the file extension.\n"
"\n", argv[0]);
return 1;
}
in_filename = argv[1];
out_filename = argv[2];
av_register_all();
AVFormatContext *ifmt_ctx = NULL;
if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
fprintf(stderr, "Could not open input file '%s'", in_filename);
goto end;
}
if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
fprintf(stderr, "Failed to retrieve input stream information");
goto end;
}
av_dump_format(ifmt_ctx, 0, in_filename, 0);
AVFormatContext *ofmt_ctx = NULL;
//根据文件后缀名创建输出的上下文
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
if (!ofmt_ctx) {
fprintf(stderr, "Could not create output context\n");
ret = AVERROR_UNKNOWN;
goto end;
}
stream_mapping_size = ifmt_ctx->nb_streams;
int *stream_mapping = (int *)av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping));
if (!stream_mapping)
{
ret = AVERROR(ENOMEM);
goto end;
}
ofmt = ofmt_ctx->oformat;
for (i = 0; i < ifmt_ctx->nb_streams; i++)
{
AVStream *in_stream = ifmt_ctx->streams[i];
AVCodecParameters *in_codecpar = in_stream->codecpar;
if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
{
stream_mapping[i] = -1;
continue;
}
stream_mapping[i] = stream_index++;
//out_stream == ofmt_ctx->stream
AVStream *out_stream = avformat_new_stream(ofmt_ctx, NULL);
if (!out_stream)
{
fprintf(stderr, "Failed allocating output stream\n");
ret = AVERROR_UNKNOWN;
goto end;
}
ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
if (ret < 0)
{
fprintf(stderr, "Failed to copy codec parameters\n");
goto end;
}
out_stream->codecpar->codec_tag = 0;
}
av_dump_format(ofmt_ctx, 0, out_filename, 1);
if (!(ofmt->flags & AVFMT_NOFILE))
{
ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
if (ret < 0)
{
fprintf(stderr, "Could not open output file '%s'", out_filename);
goto end;
}
}
//1.写头到输出文件上下文
ret = avformat_write_header(ofmt_ctx, NULL);
if (ret < 0)
{
fprintf(stderr, "Error occurred when opening output file\n");
goto end;
}
//2.读取原来的多媒体文件的数据包,写到输出文件
while (1)
{
AVStream *in_stream, *out_stream;
AVPacket pkt;
ret = av_read_frame(ifmt_ctx, &pkt);
if (ret < 0)
break;
in_stream = ifmt_ctx->streams[pkt.stream_index];
if (pkt.stream_index >= stream_mapping_size || stream_mapping[pkt.stream_index] < 0)
{
av_packet_unref(&pkt);
continue;
}
pkt.stream_index = stream_mapping[pkt.stream_index];
out_stream = ofmt_ctx->streams[pkt.stream_index];
/* copy packet */
pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AVRounding(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, AVRounding(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
av_log(NULL, AV_LOG_DEBUG, "pts = %d\n", pkt.pts);
av_log(NULL, AV_LOG_DEBUG, "dts = %d\n", pkt.dts);
pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
pkt.pos = -1;
ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
if (ret < 0) {
fprintf(stderr, "Error muxing packet\n");
break;
}
av_packet_unref(&pkt);
}
//3.写尾
av_write_trailer(ofmt_ctx);
end:
avformat_close_input(&ifmt_ctx);
/* close output */
if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
avio_closep(&ofmt_ctx->pb);
avformat_free_context(ofmt_ctx);
av_freep(&stream_mapping);
return 0;
}
6. H264 编码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
}
int main(int argc, char **argv)
{
const char *filename, *codec_name;
const AVCodec *codec;
AVCodecContext *c= NULL;
int i, ret, x, y, got_output;
FILE *f;
AVFrame *frame;
AVPacket pkt;
uint8_t endcode[] = { 0, 0, 1, 0xb7 };
filename = "/home/lili/1.h264";
codec_name = "libx264";
avcodec_register_all();
/* find the mpeg1video encoder */
codec = avcodec_find_encoder_by_name(codec_name);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
c = avcodec_alloc_context3(codec);
if (!c) {
fprintf(stderr, "Could not allocate video codec context\n");
exit(1);
}
/* put sample parameters */
c->bit_rate = 400000;
/* resolution must be a multiple of two */
c->width = 640;
c->height = 480;
/* frames per second */
c->time_base = (AVRational){1, 25};
c->framerate = (AVRational){25, 1};
/* emit one intra frame every ten frames
* check frame pict_type before passing frame
* to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
* then gop_size is ignored and the output of encoder
* will always be I frame irrespective to gop_size
*/
c->gop_size = 10;
c->max_b_frames = 1;
c->pix_fmt = AV_PIX_FMT_YUV422P;
if (codec->id == AV_CODEC_ID_H264)
av_opt_set(c->priv_data, "preset", "slow", 0);
/* open it */
if (avcodec_open2(c, codec, NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
exit(1);
}
f = fopen(filename, "wb");
if (!f) {
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "Could not allocate video frame\n");
exit(1);
}
frame->format = c->pix_fmt;
frame->width = c->width;
frame->height = c->height;
ret = av_frame_get_buffer(frame, 32);
if (ret < 0) {
fprintf(stderr, "Could not allocate the video frame data\n");
exit(1);
}
/* encode 1 second of video */
for (i = 0; i < 250; i++) {
av_init_packet(&pkt);
pkt.data = NULL; // packet data will be allocated by the encoder
pkt.size = 0;
fflush(stdout);
/* make sure the frame data is writable */
ret = av_frame_make_writable(frame);
if (ret < 0)
exit(1);
/* prepare a dummy image */
/* Y */
for (y = 0; y < c->height; y++) {
for (x = 0; x < c->width; x++) {
frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
}
}
/* Cb and Cr */
for (y = 0; y < c->height/2; y++) {
for (x = 0; x < c->width/2; x++) {
frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
}
}
frame->pts = i;
/* encode the image */
ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
if (ret < 0) {
fprintf(stderr, "Error encoding frame\n");
exit(1);
}
if (got_output) {
printf("Write frame %3d (size=%5d)\n", i, pkt.size);
fwrite(pkt.data, 1, pkt.size, f);
av_packet_unref(&pkt);
}
}
/* get the delayed frames */
for (got_output = 1; got_output; i++) {
fflush(stdout);
ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
if (ret < 0) {
fprintf(stderr, "Error encoding frame\n");
exit(1);
}
if (got_output) {
printf("Write frame %3d (size=%5d)\n", i, pkt.size);
fwrite(pkt.data, 1, pkt.size, f);
av_packet_unref(&pkt);
}
}
/* add sequence end code to have a real MPEG file */
fwrite(endcode, 1, sizeof(endcode), f);
fclose(f);
avcodec_free_context(&c);
av_frame_free(&frame);
return 0;
}