笔记:ffmpeg使用实例(bgr->yuv->h264, pcm->aac,加入音频重采样)
2017-03-27 12:07
501 查看
郑重声明:程序不完善,代码仅供参考。
#include "stdafx.h"
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "SDL2/SDL.h"
};
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "swscale.lib")
#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "SDL2.lib")
#pragma comment(lib, "SDL2main.lib")
#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48KHZ 32bit audio -> 32bit / 8 * 48000 * 1 = 192000 Byte
int main(int argc, char* argv[])
{
AVFormatContext* ofmt_ctx_h264 = NULL;
const char* output_filepath_h264 = NULL;
AVFormatContext* ofmt_ctx_aac = NULL;
const char* output_filepath_aac = NULL;
AVStream* ostream_video = NULL;
AVStream* ostream_audio = NULL;
AVCodecContext*
codec_ctx_2h264 = NULL;
AVCodec* codec_2h264 = NULL;
AVCodecContext*
codec_ctx_2aac = NULL;
AVCodec* codec_2aac = NULL;
uint8_t* video_buffer_in = NULL;
int video_buffer_size_in = -1;
uint8_t* video_buffer_out = NULL;
int video_buffer_size_out = -1;
struct SwsContext* video_convert_ctx = NULL;
uint8_t* audio_buffer_in = NULL;
int audio_buffer_size_in = -1;
uint8_t* audio_buffer_out = NULL;
int audio_buffer_size_out = -1;
struct SwrContext* audio_convert_ctx = NULL;
AVFrame frame_video = { 0 };
AVFrame frame_audio = { 0 };
AVPacket pkt_video = { 0 };
AVPacket pkt_audio = { 0 };
av_register_all();
avformat_network_init();
output_filepath_h264 = "output.h264";
output_filepath_aac = "output.aac";
if (avformat_alloc_output_context2(&ofmt_ctx_h264, NULL, "h264", output_filepath_h264) < 0)
{
return -1;
}
if (avio_open(&ofmt_ctx_h264->pb, output_filepath_h264, AVIO_FLAG_READ_WRITE) < 0)
{
return -1;
}
if (avformat_alloc_output_context2(&ofmt_ctx_aac, NULL, "adts", output_filepath_aac) < 0)
{
return -1;
}
if (avio_open(&ofmt_ctx_aac->pb, output_filepath_aac, AVIO_FLAG_READ_WRITE) < 0)
{
return - 1;
}
ostream_video = avformat_new_stream(ofmt_ctx_h264, NULL);
ostream_audio = avformat_new_stream(ofmt_ctx_aac, NULL);
ostream_audio->codec->codec_tag = 0;
if (ofmt_ctx_aac->oformat->flags & AVFMT_GLOBALHEADER)
ostream_audio->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
ostream_video->codec->codec_tag = 0;
if (ofmt_ctx_h264->oformat->flags & AVFMT_GLOBALHEADER)
ostream_video->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
codec_ctx_2h264 = ostream_video->codec;
codec_ctx_2h264->codec_id = ofmt_ctx_h264->oformat->video_codec;
codec_ctx_2h264->codec_type = AVMEDIA_TYPE_VIDEO;
codec_ctx_2h264->pix_fmt = AV_PIX_FMT_YUV420P;
codec_ctx_2h264->width = 1920;
codec_ctx_2h264->height = 1080;
codec_ctx_2h264->bit_rate = 400000;
codec_ctx_2h264->gop_size = 25;
codec_ctx_2h264->time_base.num = 1;
codec_ctx_2h264->time_base.den = 25;
codec_ctx_2h264->qmin = 10;
codec_ctx_2h264->qmax = 51;
codec_ctx_2h264->max_b_frames = 3;
AVDictionary* param = NULL;
av_dict_set(¶m, "preset", "slow", 0);
av_dict_set(¶m, "tu
bed6
ne", "zerolatency", 0);
//av_dict_set(¶m, "profile", "main", 0);
codec_2h264 = avcodec_find_encoder(codec_ctx_2h264->codec_id);
if (avcodec_open2(codec_ctx_2h264, codec_2h264, ¶m) < 0)
{
return -1;
}
codec_ctx_2aac = ostream_audio->codec;
codec_ctx_2aac->codec_id = ofmt_ctx_aac->oformat->audio_codec;
codec_ctx_2aac->codec_type = AVMEDIA_TYPE_AUDIO;
codec_ctx_2aac->sample_fmt = AV_SAMPLE_FMT_FLTP;
// 音频编解码只能使用此格式?
codec_ctx_2aac->sample_rate = 44100;
codec_ctx_2aac->bit_rate = 64000;
codec_ctx_2aac->channel_layout = AV_CH_LAYOUT_MONO;
//
codec_ctx_2aac->channels = av_get_channel_layout_nb_channels(codec_ctx_2aac->channel_layout);
codec_2aac = avcodec_find_encoder(codec_ctx_2aac->codec_id);
if (avcodec_open2(codec_ctx_2aac, codec_2aac, NULL) < 0)
{
return -1;
}
if (avformat_write_header(ofmt_ctx_h264, NULL) < 0)
{
return -1;
}
if (avformat_write_header(ofmt_ctx_aac, NULL) < 0)
{
return -1;
}
// video
int video_width_in = 1920;
int video_height_in = 1080;
AVPixelFormat video_format_in = AV_PIX_FMT_BGR24;
video_buffer_size_in = avpicture_get_size(video_format_in, video_width_in, video_height_in);
video_buffer_in = (uint8_t*)av_malloc(video_buffer_size_in);
frame_video.width = codec_ctx_2h264->width;
frame_video.height = codec_ctx_2h264->height;
frame_video.format = codec_ctx_2h264->pix_fmt;
video_buffer_size_out = avpicture_get_size((AVPixelFormat)frame_video.format, frame_video.width, frame_video.height);
video_buffer_out = (uint8_t*)av_malloc(video_buffer_size_out);
avpicture_fill((AVPicture*)&frame_video, video_buffer_out, (AVPixelFormat)frame_video.format, frame_video.width, frame_video.height);
video_convert_ctx = sws_getContext(video_width_in, video_height_in, video_format_in,
frame_video.width, frame_video.height, (AVPixelFormat)frame_video.format, SWS_BICUBIC, NULL, NULL, NULL);
// audio
int64_t audio_channel_layout_in = AV_CH_LAYOUT_STEREO;
int audio_sample_rate_in = 44100;
int audio_nb_samples_in = 1024;
AVSampleFormat audio_sample_fmt_in = AV_SAMPLE_FMT_S16;
int audio_channels_in = av_get_channel_layout_nb_channels(audio_channel_layout_in);
audio_buffer_size_in = av_samples_get_buffer_size(NULL, audio_channels_in, audio_nb_samples_in, audio_sample_fmt_in, 1);
audio_buffer_in = (uint8_t*)av_malloc(audio_buffer_size_in);
frame_audio.channel_layout = codec_ctx_2aac->channel_layout;
frame_audio.nb_samples = codec_ctx_2aac->frame_size;
frame_audio.channels = codec_ctx_2aac->channels;
frame_audio.format = codec_ctx_2aac->sample_fmt;
frame_audio.sample_rate = codec_ctx_2aac->sample_rate;
audio_buffer_size_out = av_samples_get_buffer_size(NULL, frame_audio.channels, frame_audio.nb_samples, (AVSampleFormat)frame_audio.format, 1);
audio_buffer_out = (uint8_t*)av_malloc(audio_buffer_size_out);
avcodec_fill_audio_frame(&frame_audio, frame_audio.channels, (AVSampleFormat)frame_audio.format, (const uint8_t*)audio_buffer_out, audio_buffer_size_out, 1);
audio_convert_ctx = swr_alloc_set_opts(audio_convert_ctx, frame_audio.channel_layout, (AVSampleFormat)frame_audio.format, frame_audio.sample_rate,
audio_channel_layout_in, audio_sample_fmt_in, audio_sample_rate_in, 0, NULL);
swr_init(audio_convert_ctx);
av_init_packet(&pkt_audio);
pkt_audio.data = NULL;
pkt_audio.size = 0;
av_init_packet(&pkt_video);
pkt_video.data = NULL;
pkt_video.size = 0;
FILE* input_pcm_after = fopen("input_audio_s16_stereo.pcm", "rb");
FILE* input_bgr = fopen("input_video.bgr", "rb");
while (1)
{
if (fread(video_buffer_in, video_buffer_size_in, 1, input_bgr) > 0)
{
static int videocnt = 0;
printf("videocnt %d\n", videocnt++);
int video_stride_in = video_width_in * 3;
sws_scale(video_convert_ctx, (const uint8_t* const*)&video_buffer_in, &video_stride_in, 0, video_height_in, frame_video.data, frame_video.linesize);
int got_picture = 0;
//frame_yuv.pts = videocnt;
if (avcodec_encode_video2(codec_ctx_2h264, &pkt_video, &frame_video, &got_picture) < 0)
{
return -1;
}
if (got_picture == 1)
{
pkt_video.stream_index = 0;
av_write_frame(ofmt_ctx_h264, &pkt_video);
av_free_packet(&pkt_video);
}
}
if (fread(audio_buffer_in, audio_buffer_size_in, 1, input_pcm_after) > 0)
{
static int audiocnt = 0;
printf("audiocnt %d\n", audiocnt++);
swr_convert(audio_convert_ctx, &audio_buffer_out, MAX_AUDIO_FRAME_SIZE, (const uint8_t**)&audio_buffer_in, audio_nb_samples_in);
frame_audio.data[0] = audio_buffer_out;
frame_audio.pts = audiocnt;
int got_frame = 0;
if (avcodec_encode_audio2(codec_ctx_2aac, &pkt_audio, &frame_audio, &got_frame) < 0)
{
return -1;
}
if (got_frame == 1)
{
pkt_audio.stream_index = 0;
av_write_frame(ofmt_ctx_aac, &pkt_audio);
av_free_packet(&pkt_audio);
}
}
}
return 0;
}
#include "stdafx.h"
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "SDL2/SDL.h"
};
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "swscale.lib")
#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "SDL2.lib")
#pragma comment(lib, "SDL2main.lib")
#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48KHZ 32bit audio -> 32bit / 8 * 48000 * 1 = 192000 Byte
int main(int argc, char* argv[])
{
AVFormatContext* ofmt_ctx_h264 = NULL;
const char* output_filepath_h264 = NULL;
AVFormatContext* ofmt_ctx_aac = NULL;
const char* output_filepath_aac = NULL;
AVStream* ostream_video = NULL;
AVStream* ostream_audio = NULL;
AVCodecContext*
codec_ctx_2h264 = NULL;
AVCodec* codec_2h264 = NULL;
AVCodecContext*
codec_ctx_2aac = NULL;
AVCodec* codec_2aac = NULL;
uint8_t* video_buffer_in = NULL;
int video_buffer_size_in = -1;
uint8_t* video_buffer_out = NULL;
int video_buffer_size_out = -1;
struct SwsContext* video_convert_ctx = NULL;
uint8_t* audio_buffer_in = NULL;
int audio_buffer_size_in = -1;
uint8_t* audio_buffer_out = NULL;
int audio_buffer_size_out = -1;
struct SwrContext* audio_convert_ctx = NULL;
AVFrame frame_video = { 0 };
AVFrame frame_audio = { 0 };
AVPacket pkt_video = { 0 };
AVPacket pkt_audio = { 0 };
av_register_all();
avformat_network_init();
output_filepath_h264 = "output.h264";
output_filepath_aac = "output.aac";
if (avformat_alloc_output_context2(&ofmt_ctx_h264, NULL, "h264", output_filepath_h264) < 0)
{
return -1;
}
if (avio_open(&ofmt_ctx_h264->pb, output_filepath_h264, AVIO_FLAG_READ_WRITE) < 0)
{
return -1;
}
if (avformat_alloc_output_context2(&ofmt_ctx_aac, NULL, "adts", output_filepath_aac) < 0)
{
return -1;
}
if (avio_open(&ofmt_ctx_aac->pb, output_filepath_aac, AVIO_FLAG_READ_WRITE) < 0)
{
return - 1;
}
ostream_video = avformat_new_stream(ofmt_ctx_h264, NULL);
ostream_audio = avformat_new_stream(ofmt_ctx_aac, NULL);
ostream_audio->codec->codec_tag = 0;
if (ofmt_ctx_aac->oformat->flags & AVFMT_GLOBALHEADER)
ostream_audio->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
ostream_video->codec->codec_tag = 0;
if (ofmt_ctx_h264->oformat->flags & AVFMT_GLOBALHEADER)
ostream_video->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
codec_ctx_2h264 = ostream_video->codec;
codec_ctx_2h264->codec_id = ofmt_ctx_h264->oformat->video_codec;
codec_ctx_2h264->codec_type = AVMEDIA_TYPE_VIDEO;
codec_ctx_2h264->pix_fmt = AV_PIX_FMT_YUV420P;
codec_ctx_2h264->width = 1920;
codec_ctx_2h264->height = 1080;
codec_ctx_2h264->bit_rate = 400000;
codec_ctx_2h264->gop_size = 25;
codec_ctx_2h264->time_base.num = 1;
codec_ctx_2h264->time_base.den = 25;
codec_ctx_2h264->qmin = 10;
codec_ctx_2h264->qmax = 51;
codec_ctx_2h264->max_b_frames = 3;
AVDictionary* param = NULL;
av_dict_set(¶m, "preset", "slow", 0);
av_dict_set(¶m, "tu
bed6
ne", "zerolatency", 0);
//av_dict_set(¶m, "profile", "main", 0);
codec_2h264 = avcodec_find_encoder(codec_ctx_2h264->codec_id);
if (avcodec_open2(codec_ctx_2h264, codec_2h264, ¶m) < 0)
{
return -1;
}
codec_ctx_2aac = ostream_audio->codec;
codec_ctx_2aac->codec_id = ofmt_ctx_aac->oformat->audio_codec;
codec_ctx_2aac->codec_type = AVMEDIA_TYPE_AUDIO;
codec_ctx_2aac->sample_fmt = AV_SAMPLE_FMT_FLTP;
// 音频编解码只能使用此格式?
codec_ctx_2aac->sample_rate = 44100;
codec_ctx_2aac->bit_rate = 64000;
codec_ctx_2aac->channel_layout = AV_CH_LAYOUT_MONO;
//
codec_ctx_2aac->channels = av_get_channel_layout_nb_channels(codec_ctx_2aac->channel_layout);
codec_2aac = avcodec_find_encoder(codec_ctx_2aac->codec_id);
if (avcodec_open2(codec_ctx_2aac, codec_2aac, NULL) < 0)
{
return -1;
}
if (avformat_write_header(ofmt_ctx_h264, NULL) < 0)
{
return -1;
}
if (avformat_write_header(ofmt_ctx_aac, NULL) < 0)
{
return -1;
}
// video
int video_width_in = 1920;
int video_height_in = 1080;
AVPixelFormat video_format_in = AV_PIX_FMT_BGR24;
video_buffer_size_in = avpicture_get_size(video_format_in, video_width_in, video_height_in);
video_buffer_in = (uint8_t*)av_malloc(video_buffer_size_in);
frame_video.width = codec_ctx_2h264->width;
frame_video.height = codec_ctx_2h264->height;
frame_video.format = codec_ctx_2h264->pix_fmt;
video_buffer_size_out = avpicture_get_size((AVPixelFormat)frame_video.format, frame_video.width, frame_video.height);
video_buffer_out = (uint8_t*)av_malloc(video_buffer_size_out);
avpicture_fill((AVPicture*)&frame_video, video_buffer_out, (AVPixelFormat)frame_video.format, frame_video.width, frame_video.height);
video_convert_ctx = sws_getContext(video_width_in, video_height_in, video_format_in,
frame_video.width, frame_video.height, (AVPixelFormat)frame_video.format, SWS_BICUBIC, NULL, NULL, NULL);
// audio
int64_t audio_channel_layout_in = AV_CH_LAYOUT_STEREO;
int audio_sample_rate_in = 44100;
int audio_nb_samples_in = 1024;
AVSampleFormat audio_sample_fmt_in = AV_SAMPLE_FMT_S16;
int audio_channels_in = av_get_channel_layout_nb_channels(audio_channel_layout_in);
audio_buffer_size_in = av_samples_get_buffer_size(NULL, audio_channels_in, audio_nb_samples_in, audio_sample_fmt_in, 1);
audio_buffer_in = (uint8_t*)av_malloc(audio_buffer_size_in);
frame_audio.channel_layout = codec_ctx_2aac->channel_layout;
frame_audio.nb_samples = codec_ctx_2aac->frame_size;
frame_audio.channels = codec_ctx_2aac->channels;
frame_audio.format = codec_ctx_2aac->sample_fmt;
frame_audio.sample_rate = codec_ctx_2aac->sample_rate;
audio_buffer_size_out = av_samples_get_buffer_size(NULL, frame_audio.channels, frame_audio.nb_samples, (AVSampleFormat)frame_audio.format, 1);
audio_buffer_out = (uint8_t*)av_malloc(audio_buffer_size_out);
avcodec_fill_audio_frame(&frame_audio, frame_audio.channels, (AVSampleFormat)frame_audio.format, (const uint8_t*)audio_buffer_out, audio_buffer_size_out, 1);
audio_convert_ctx = swr_alloc_set_opts(audio_convert_ctx, frame_audio.channel_layout, (AVSampleFormat)frame_audio.format, frame_audio.sample_rate,
audio_channel_layout_in, audio_sample_fmt_in, audio_sample_rate_in, 0, NULL);
swr_init(audio_convert_ctx);
av_init_packet(&pkt_audio);
pkt_audio.data = NULL;
pkt_audio.size = 0;
av_init_packet(&pkt_video);
pkt_video.data = NULL;
pkt_video.size = 0;
FILE* input_pcm_after = fopen("input_audio_s16_stereo.pcm", "rb");
FILE* input_bgr = fopen("input_video.bgr", "rb");
while (1)
{
if (fread(video_buffer_in, video_buffer_size_in, 1, input_bgr) > 0)
{
static int videocnt = 0;
printf("videocnt %d\n", videocnt++);
int video_stride_in = video_width_in * 3;
sws_scale(video_convert_ctx, (const uint8_t* const*)&video_buffer_in, &video_stride_in, 0, video_height_in, frame_video.data, frame_video.linesize);
int got_picture = 0;
//frame_yuv.pts = videocnt;
if (avcodec_encode_video2(codec_ctx_2h264, &pkt_video, &frame_video, &got_picture) < 0)
{
return -1;
}
if (got_picture == 1)
{
pkt_video.stream_index = 0;
av_write_frame(ofmt_ctx_h264, &pkt_video);
av_free_packet(&pkt_video);
}
}
if (fread(audio_buffer_in, audio_buffer_size_in, 1, input_pcm_after) > 0)
{
static int audiocnt = 0;
printf("audiocnt %d\n", audiocnt++);
swr_convert(audio_convert_ctx, &audio_buffer_out, MAX_AUDIO_FRAME_SIZE, (const uint8_t**)&audio_buffer_in, audio_nb_samples_in);
frame_audio.data[0] = audio_buffer_out;
frame_audio.pts = audiocnt;
int got_frame = 0;
if (avcodec_encode_audio2(codec_ctx_2aac, &pkt_audio, &frame_audio, &got_frame) < 0)
{
return -1;
}
if (got_frame == 1)
{
pkt_audio.stream_index = 0;
av_write_frame(ofmt_ctx_aac, &pkt_audio);
av_free_packet(&pkt_audio);
}
}
}
return 0;
}
相关文章推荐
- 笔记:ffmpeg使用实例:bgr->yuv->h264,pcm->aac,没有音频重采样
- 笔记:ffmpeg使用实例:yuv->h264,pcm->aac
- 笔记:ffmpeg使用实例:rgb + pcm --> flv(音频编码+视频编码+muxer)
- ffmpeg实战教程(三)音频PCM采样为AAC,视频YUV编码为H264/HEVC
- FFmpeg 音频编码(PCM数据编码成AAC android)
- Live555接收h264使用ffmpeg解码为YUV420
- 使用ffmpeg将BMP图片编码为x264视频文件,将H264视频保存为BMP图片,yuv视频文件保存为图片的代码
- Live555接收h264使用ffmpeg解码为YUV420 .
- Android FFMpeg(三)——使用FFMpeg解码h264、aac
- 使用ffmpeg将yuv编码成h264时有大小限制,太小的数据源编码会失败。
- 流媒体技术学习笔记之(六)FFmpeg官方文档先进音频编码(AAC)
- 使用mp4v2将aac音频h264视频数据封装成mp4开发心得
- 使用ffmpeg解码音频文件到PCM格式
- 使用ffmpeg将BMP图片编码为x264视频文件,将H264视频保存为BMP图片,yuv视频文件保存为图片的代码
- 使用mp4v2将aac音频h264视频数据封装成mp4开发心得
- ffmpeg的h264转yuv实例测试代码
- ibatis学习笔记(四)>>>>>>>ibatis使用实例
- ibatis学习笔记(四)>>>>>>>ibatis使用实例
- FFMPEG:压缩之H264编码(YUV420P->H264) 分类: DirectX ffmpeg-SDL-VLC-Live555 2014-11-26 14:31 997人阅读 评论(0) 收藏
- 最简单的基于FFmpeg的音频编码器 PCM 转AAC