/*
* 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>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
/* 5 seconds stream duration */
#define STREAM_DURATION 5.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 = codec_id;
c->codec_type = CODEC_TYPE_AUDIO;
/* put sample parameters */
c->bit_rate = 64000;
c->sample_rate = 44100;
c->channels = 2;
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 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 = 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 = 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);
pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
pkt.flags |= 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 = codec_id;
c->codec_type = CODEC_TYPE_VIDEO;
/* 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 coeffs overflow
this doesnt happen with normal video, it just happens here as the
motion of the chroma plane doesnt match the luma plane */
c->mb_decision=2;
}
// 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 AVFrame *alloc_picture(int pix_fmt, int width, int height) {
AVFrame *picture;
uint8_t *picture_buf;
int size;
picture = avcodec_alloc_frame();
if (!picture)
return NULL;
size = avpicture_get_size(pix_fmt, width, height);
picture_buf = av_malloc(size);
if (!picture_buf) {
av_free(picture);
return NULL;
}
avpicture_fill((AVPicture *)picture, picture_buf,
pix_fmt, width, height);
return picture;
}
static void open_video(AVFormatContext *oc, AVStream *st) {
AVCodec *codec;
AVCodecContext *c;
c = st->codec;
/* find the video encoder */
codec = avcodec_find_encoder(c->codec_id);
if (!codec) {
fprintf(stderr, "codec not found\n");
exit(1);
}
/* open the codec */
if (avcodec_open(c, codec) < 0) {
fprintf(stderr, "could not open codec\n");
exit(1);
}
video_outbuf = NULL;
if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
/* allocate output buffer */
/* XXX: API change will be done */
/* buffers passed into lav* can be allocated any way you prefer,
as long as the
没有合适的资源?快使用搜索试试~ 我知道了~
在VS2008下调试output_example
共74个文件
h:28个
dll:8个
pc:6个
5星 · 超过95%的资源 需积分: 9 12 下载量 158 浏览量
2011-10-18
00:18:35
上传
评论
收藏 6.04MB RAR 举报
温馨提示
完全照文档, 8步:修改编译输出路径,为什么? 9步:工程文件夹下的Debug文件夹下——实际是:OutputExample2\Debug\OutputExample2 ? release版 要按照debug设置,且dll放入releas文件夹 cmd运行 OutputExample.exe xxx.format 貌似自动转avi
资源推荐
资源详情
资源评论
收起资源包目录
OutputExample2.rar (74个子文件)
OutputExample2
Debug
OutputExample2
Debug 38KB
swscale.dll 155KB
avutil.dll 56KB
Debug.ilk 400KB
avformat.dll 651KB
Debug.pdb 475KB
avcodec.dll 6.94MB
OutputExample2.ncb 1.1MB
Release
OutputExample2.pdb 259KB
swscale.dll 155KB
avutil.dll 56KB
avformat.dll 651KB
avcodec.dll 6.94MB
paradox.avi 458KB
OutputExample2.exe 12KB
OutputExample2.sln 908B
OutputExample2
libavcodec
opt.h 3KB
avcodec.h 96KB
Debug
vc90.pdb 84KB
vc90.idb 59KB
BuildLog.htm 10KB
mt.dep 67B
Debug.embed.manifest 663B
Debug.embed.manifest.res 728B
output_example.obj 33KB
Debug.intermediate.manifest 621B
OutputExample2.vcproj 6KB
inttypes.h 91B
lib
avformat.lib 856KB
avcodec.lib 569KB
avdevice.lib 890KB
swscale.lib 39KB
pkgconfig
libavdevice.pc 478B
libavutil.pc 249B
libavformat.pc 479B
libswscale.pc 276B
libavfilter.pc 474B
libavcodec.pc 464B
avutil.lib 18KB
avfilter.lib 921KB
libavfilter
avfilter.h 24KB
libavdevice
avdevice.h 2KB
output_example.c 16KB
stdint.h 4KB
Release
vc90.pdb 84KB
vc90.idb 59KB
BuildLog.htm 10KB
mt.dep 67B
output_example.obj 39KB
OutputExample2.exe.intermediate.manifest 616B
libswscale
rgb2rgb.h 7KB
swscale.h 6KB
libavutil
lzo.h 1KB
base64.h 1KB
mathematics.h 2KB
sha1.h 1KB
log.h 3KB
avstring.h 3KB
mem.h 4KB
fifo.h 4KB
common.h 11KB
avutil.h 7KB
random.h 2KB
md5.h 1KB
rational.h 3KB
intfloat_readwrite.h 1KB
crc.h 1KB
adler32.h 1KB
libavformat
rtsp.h 3KB
rtspcodes.h 2KB
avio.h 13KB
avformat.h 37KB
OutputExample2.vcproj.Susan.huangyue.user 1KB
OutputExample2.suo 24KB
共 74 条
- 1
资源评论
- 空空大人2012-12-19不错,步骤很详细,最近正在学习ffmpeg
OnlyLonly
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功