Note that this library only handles codecs (mpeg, mpeg4, etc...), not file formats (avi, vob, etc...). See library 'libavformat' for the format handling
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_AV_CONFIG_H
#undef HAVE_AV_CONFIG_H
#endif
#define INBUF_SIZE 4096
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096
{
int frame_size, i, j, out_size, outbuf_size;
uint8_t *outbuf;
if (!codec) {
fprintf(stderr,
"codec not found\n");
}
if (avcodec_open(c, codec) < 0) {
fprintf(stderr,
"could not open codec\n");
}
outbuf_size = 10000;
f = fopen(filename, "wb");
if (!f) {
fprintf(stderr,
"could not open %s\n", filename);
}
t = 0;
for(i=0;i<200;i++) {
for(j=0;j<frame_size;j++) {
samples[2*j] = (int)(sin(t) * 10000);
samples[2*j+1] = samples[2*j];
}
out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
fwrite(outbuf, 1, out_size, f);
}
fclose(f);
}
{
if (!codec) {
fprintf(stderr,
"codec not found\n");
}
if (avcodec_open(c, codec) < 0) {
fprintf(stderr,
"could not open codec\n");
}
f = fopen(filename, "rb");
if (!f) {
fprintf(stderr,
"could not open %s\n", filename);
}
outfile = fopen(outfilename, "wb");
if (!outfile) {
}
int got_frame = 0;
if (!decoded_frame) {
fprintf(stderr,
"out of memory\n");
}
} else
if (len < 0) {
fprintf(stderr,
"Error while decoding\n");
}
if (got_frame) {
fwrite(decoded_frame->
data[0], 1, data_size, outfile);
}
if (len > 0)
}
}
fclose(outfile);
fclose(f);
}
{
int i, out_size,
size, x, y, outbuf_size;
uint8_t *outbuf, *picture_buf;
if (!codec) {
fprintf(stderr,
"codec not found\n");
}
if (avcodec_open(c, codec) < 0) {
fprintf(stderr,
"could not open codec\n");
}
f = fopen(filename, "wb");
if (!f) {
fprintf(stderr,
"could not open %s\n", filename);
}
outbuf_size = 100000;
picture_buf =
malloc((size * 3) / 2);
picture->
data[0] = picture_buf;
picture->
data[2] = picture->
data[1] + size / 4;
for(i=0;i<25;i++) {
fflush(stdout);
for(x=0;x<c->
width;x++) {
picture->
data[0][y * picture->
linesize[0] + x] = x + y + i * 3;
}
}
for(x=0;x<c->
width/2;x++) {
picture->
data[1][y * picture->
linesize[1] + x] = 128 + y + i * 2;
picture->
data[2][y * picture->
linesize[2] + x] = 64 + x + i * 5;
}
}
printf(
"encoding frame %3d (size=%5d)\n", i, out_size);
fwrite(outbuf, 1, out_size, f);
}
for(; out_size; i++) {
fflush(stdout);
printf(
"write frame %3d (size=%5d)\n", i, out_size);
fwrite(outbuf, 1, out_size, f);
}
outbuf[0] = 0x00;
outbuf[1] = 0x00;
outbuf[2] = 0x01;
outbuf[3] = 0xb7;
fwrite(outbuf, 1, 4, f);
fclose(f);
}
static void pgm_save(
unsigned char *buf,
int wrap,
int xsize,
int ysize,
char *filename)
{
int i;
f=fopen(filename,"w");
fprintf(f,
"P5\n%d %d\n%d\n",xsize,ysize,255);
for(i=0;i<ysize;i++)
fwrite(buf + i * wrap,1,xsize,f);
fclose(f);
}
{
int frame, got_picture,
len;
char buf[1024];
if (!codec) {
fprintf(stderr,
"codec not found\n");
}
if (avcodec_open(c, codec) < 0) {
fprintf(stderr,
"could not open codec\n");
}
f = fopen(filename, "rb");
if (!f) {
fprintf(stderr,
"could not open %s\n", filename);
}
frame = 0;
for(;;) {
break;
if (len < 0) {
fprintf(stderr,
"Error while decoding frame %d\n", frame);
}
if (got_picture) {
printf(
"saving frame %3d\n", frame);
fflush(stdout);
snprintf(buf, sizeof(buf), outfilename, frame);
frame++;
}
}
}
if (got_picture) {
printf(
"saving last frame %3d\n", frame);
fflush(stdout);
snprintf(buf, sizeof(buf), outfilename, frame);
frame++;
}
fclose(f);
}
int main(
int argc,
char **argv)
{
const char *filename;
if (argc <= 1) {
filename = "/tmp/test.mpg";
} else {
filename = argv[1];
}
return 0;
}