/*
* Libavformat API example: Output a media file in any supported
* libavformat format. The default codecs are used.
*
* Copyright (c) 2003 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
//#include <time.h>
#include <Windows.h>
/*
* Libavformat API example: Output a media file in any supported
* libavformat format. The default codecs are used.
*
* Copyright (c) 2003 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
//#include <time.h>
#include <Windows.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
extern "C" {
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
};
#undef exit
/* 5 seconds stream duration */
#define STREAM_DURATION 50.0
#define STREAM_FRAME_RATE 25 /* 25 images/s */
#define STREAM_NB_FRAMES ((int )(STREAM_DURATION * STREAM_FRAME_RATE ))
#define STREAM_PIX_FMT PIX_FMT_YUV420P /* default pix_fmt */
static int sws_flags = SWS_BICUBIC;
/**************************************************************/
/* audio output */
float t, tincr, tincr2;
int16_t *samples;
uint8_t *audio_outbuf;
int audio_outbuf_size;
int audio_input_frame_size;
/*
* add an audio output stream
*/
static AVStream *add_audio_stream(AVFormatContext *oc, int codec_id)
{
AVCodecContext * c;
AVStream * st;
st = av_new_stream(oc, 1);
if (!st) {
fprintf(stderr, "Could not alloc stream\n");
exit(1);
}
c = st->codec;
c->codec_id = (CodecID)codec_id;
c->codec_type = AVMEDIA_TYPE_AUDIO;
/* put sample parameters */
c->sample_fmt = AV_SAMPLE_FMT_S16;
c->sample_rate = 8000;
c->channels = 2;
c->bit_rate = 32000;
// some formats want stream headers to be separate
if (!strcmp(oc->oformat->name, "mp4") || !strcmp(oc->oformat->name, "mov") || !strcmp(oc->oformat->name, "3gp"))
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
return st;
}
static void open_audio(AVFormatContext *oc, AVStream *st)
{
AVCodecContext * c;
AVCodec * codec;
c = st->codec;
/* find the audio encoder */
codec = avcodec_find_encoder(c->codec_id);
if (!codec) {
fprintf(stderr, "codec not found\n");
exit(1);
}
/* open it */
if (avcodec_open(c, codec) < 0) {
fprintf(stderr, "could not open audio codec\n");
exit(1);
}
/* init signal generator */
t = 0;
tincr = 2 * M_PI * 110.0 / c->sample_rate;
/* increment frequency by 110 Hz per second */
tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
audio_outbuf_size = 10000;
audio_outbuf = (uint8_t *)av_malloc(audio_outbuf_size);
/* ugly hack for PCM codecs (will be removed ASAP with new PCM
support to compute the input frame size in samples */
if (c->frame_size <= 1) {
audio_input_frame_size = audio_outbuf_size / c->channels;
switch (st->codec->codec_id) {
case CODEC_ID_PCM_S16LE:
case CODEC_ID_PCM_S16BE:
case CODEC_ID_PCM_U16LE:
case CODEC_ID_PCM_U16BE:
audio_input_frame_size >>= 1;
break;
default:
break;
}
}
else {
audio_input_frame_size = c->frame_size;
}
samples = (int16_t *)av_malloc(audio_input_frame_size * 2 * c->channels);
}
/* prepare a 16 bit dummy audio frame of 'frame_size' samples and
'nb_channels' channels */
static void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
{
int j, i, v;
int16_t * q;
q = samples;
for (j = 0; j< frame_size; j++) {
v = (int)(sin(t) * 10000);
for (i = 0; i < nb_channels; i++)
* q++ = v;
t += tincr;
tincr += tincr2;
}
}
static void write_audio_frame(AVFormatContext *oc, AVStream *st)
{
AVCodecContext * c;
AVPacket pkt;
av_init_packet(&pkt);
c = st->codec;
get_audio_frame(samples, audio_input_frame_size, c->channels);
pkt.size = avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples);
printf("[audio] AAC frame len=%d\n", pkt.size);
//pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
//pkt.flags |= AV_PKT_FLAG_KEY;
pkt.stream_index = st->index;
pkt.data = audio_outbuf;
/* write the compressed frame in the media file */
if (av_write_frame(oc, &pkt) != 0) {
fprintf(stderr, "Error while writing audio frame\n");
exit(1);
}
}
static void close_audio(AVFormatContext *oc, AVStream *st)
{
avcodec_close(st->codec);
av_free(samples);
av_free(audio_outbuf);
}
/**************************************************************/
/* video output */
AVFrame *picture, *tmp_picture;
uint8_t *video_outbuf;
int frame_count, video_outbuf_size;
/* add a video output stream */
static AVStream *add_video_stream(AVFormatContext *oc, int codec_id)
{
AVCodecContext * c;
AVStream * st;
st = av_new_stream(oc, 0);
if (!st) {
fprintf(stderr, "Could not alloc stream\n");
exit(1);
}
c = st->codec;
c->codec_id = (CodecID)codec_id;
c->codec_type = AVMEDIA_TYPE_VIDEO;
c->me_range = 16;
c->max_qdiff = 4;
c->qmin = 10;
c->qmax = 51;
c->qcompress = 0.6;
/* put sample parameters */
c->bit_rate = 400000;
/* resolution must be a multiple of two */
c->width = 352;
c->height = 288;
/* time base: this is the fundamental unit of time (in seconds) in terms
of which frame timestamps are represented. for fixed-fps content,
timebase should be 1/framerate and timestamp increments should be
identically 1. */
c->time_base.den = STREAM_FRAME_RATE;
c->time_base.num = 1;
c->gop_size = 12; /* emit one intra frame every twelve frames at most */
c->pix_fmt = STREAM_PIX_FMT;
if (c->codec_id == CODEC_ID_MPEG2VIDEO) {
/* just for testing, we also add B frames */
c->max_b_frames = 2;
}
if (c->codec_id == CODEC_ID_MPEG1VIDEO){
/* Needed to avoid using macroblocks in which some coef
没有合适的资源?快使用搜索试试~ 我知道了~
ffmpeg构造声音和图像输出到Mp4
共146个文件
h:97个
lib:8个
a:7个
4星 · 超过85%的资源 需积分: 9 48 下载量 88 浏览量
2014-07-28
09:47:52
上传
评论 1
收藏 6.03MB RAR 举报
温馨提示
ffmpeg录像主要分为两部分:1、构造yuv420数据 2、构造aac声音 将图像yuv420和aac用pts和dts进行同步 然后写到mp4文件 更多原理请参考我的博客
资源推荐
资源详情
资源评论
收起资源包目录
ffmpeg构造声音和图像输出到Mp4 (146个子文件)
libavcodec.dll.a 4.37MB
libavformat.dll.a 542KB
libavutil.dll.a 102KB
libavfilter.dll.a 74KB
libswscale.dll.a 33KB
libpostproc.dll.a 6KB
libavdevice.dll.a 3KB
output_example.cpp 17KB
avcodec-53.dll 11.89MB
avformat-53.dll 1.24MB
avfilter-2.dll 780KB
swscale-2.dll 229KB
avutil-51.dll 130KB
avdevice-53.dll 54KB
output-example.exe 24KB
output-example.vcxproj.filters 1KB
SDL_opengl.h 329KB
avcodec.h 136KB
avformat.h 58KB
SDL_video.h 37KB
avfilter.h 35KB
avio.h 24KB
SDL_stdinc.h 16KB
intreadwrite.h 14KB
SDL_events.h 13KB
pixfmt.h 13KB
swscale.h 12KB
SDL_audio.h 11KB
common.h 11KB
opt.h 9KB
SDL_keysym.h 7KB
pixdesc.h 7KB
eval.h 6KB
SDL_syswm.h 6KB
SDL_endian.h 6KB
SDL_cdrom.h 6KB
SDL_mutex.h 6KB
xvmc.h 6KB
SDL_joystick.h 5KB
log.h 5KB
begin_code.h 5KB
stdint.h 5KB
SDL_rwops.h 5KB
imgutils.h 5KB
mem.h 5KB
SDL_mouse.h 5KB
avfiltergraph.h 5KB
avstring.h 5KB
samplefmt.h 5KB
SDL_config_win32.h 4KB
parseutils.h 4KB
SDL_timer.h 4KB
audioconvert.h 4KB
SDL_thread.h 4KB
SDL_config_macosx.h 4KB
SDL_keyboard.h 4KB
avutil.h 4KB
vaapi.h 4KB
rational.h 4KB
SDL_config_os2.h 4KB
fifo.h 4KB
mathematics.h 4KB
postprocess.h 3KB
SDL.h 3KB
bswap.h 3KB
SDL_config_symbian.h 3KB
error.h 3KB
attributes.h 3KB
dict.h 3KB
version.h 3KB
SDL_config_nds.h 3KB
avfft.h 3KB
SDL_main.h 3KB
vdpau.h 3KB
SDL_config_dreamcast.h 3KB
SDL_config_macos.h 3KB
SDL_loadso.h 3KB
SDL_platform.h 3KB
version.h 3KB
SDL_version.h 3KB
cpu.h 2KB
lzo.h 2KB
avcodec.h 2KB
SDL_cpuinfo.h 2KB
avassert.h 2KB
avdevice.h 2KB
SDL_quit.h 2KB
lfg.h 2KB
SDL_config_minimal.h 2KB
SDL_active.h 2KB
SDL_error.h 2KB
base64.h 2KB
dxva2.h 2KB
file.h 2KB
sha.h 2KB
aes.h 2KB
vsrc_buffer.h 2KB
vsink_buffer.h 2KB
close_code.h 1KB
crc.h 1KB
共 146 条
- 1
- 2
资源评论
- cmengwei2017-02-24官方的,是有点不厚道
- handongdong1262016-04-08谢谢分享。。。
- XZ6933391732016-03-11还不错,可以学习学习
- cnlad2015-10-26是官方的,是有点不厚道
- 千年老三2015-01-11楼主不厚道,拿官方的例子来赚分zhouyongku2015-04-07你要更多的可以参考我的博客
zhouyongku
- 粉丝: 246
- 资源: 40
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功