ffmpeg_* wrapper api for encoding/decoding w/ libav*

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

ffmpeg_* wrapper api for encoding/decoding w/ libav*

Kyle Mallory-2
For anyone interested, here is lightweight API for ffmpeg, which I am
using as the basis for my plugins.

It works, but its not super robust. I simply don't have the experience
with FFMPEG/libav* to make it bullet proof.  I'm hoping some of you
might find it useful, and in turn contribute to its stability,
robustness, and ease of use.

In lieu of posting a half-dozen small attachments, I opted for the
compressed tar ball.  If anyone would prefer the individual files, just
let me know.

It is being contributed under the Lesser Gnu Public License agreement.

Enjoy.


Kyle


For any who are curious, here is an example encoding test audio and
video (not the same as included ffmpeg_example.c):


#include "ffmpeg_codec.h"

int main(int argc, char **argv) {
        char *filename = "test.mp4";
        AVFormatContext *ctx = NULL;
        AVStream *vs = NULL, *as = NULL;
        AVRational fps = { 1001, 30000 };
        AVCodec *ac = NULL, *vc = NULL;
        AVFrame *test_video = NULL;
        void *test_audio = NULL;
        int fnum = 0;
       
        avcodec_init();
        av_register_all();
       
        AVOutputFormat *fmt = guess_format(NULL, filename, NULL);
       
        if (fmt) {
                ctx = ffmpeg_writer_init(fmt, NULL);
                if (!ctx) {
                        sprintf(stderr, "Unable to initialize writer.\n");
                        return -1;
                }
                // get the default audio and video encoders for this format
                vc = avcodec_find_encoder(fmt->video_codec);
                ac = avcodec_find_encoder(fmt->audio_codec);

                if (vc) // if a video codec is available, add a video stream and
initialize the stream encoder
                        vs = ffmpeg_add_video_stream(ctx, (char *)vc->name, 640, 480, -1,
&fps, 512); // 640x480 29.97, 512kbps
                if (ac) // if an audio codec is available, add an audio stream and
initialize the stream encoder
                        as = ffmpeg_add_audio_stream(ctx, (char *)ac->name, SAMPLE_FMT_S16,
2, 44100, 192); // 44100hz 16bit Stereo, 192kps
               
                // open the file, write format headers, etc.
                ffmpeg_writer_open(ctx, filename);
               
                // write 120 frames, approx 4.0 seconds
                for (fnum = 0; fnum < 120; fnum++) {
                        if (vs) {
                                // if there is a video stream, encode a video frame, and write it
                                test_video = ffmpeg_test_video_frame(vs->codec, fnum);
                                ffmpeg_write_frame(ctx, vs, test_video, fnum, NULL);
                                ffmpeg_free_frame(test_video);
                        }
                               
                        if (as) {
                                // if there is an audio stream, encode a frame, and write it
                                test_audio = ffmpeg_test_audio_frame(as->codec);
                                ffmpeg_write_frame(ctx, as, test_audio, fnum, NULL);
                                ffmpeg_free_frame(test_audio);
                        }
                }
               
                // after writing 120 frames, write trailers, flush buffers, free
memory, and wrap up.
                ffmpeg_writer_close(ctx);
        }
       
}


_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-user

ffmpeg_lightweight_api.tar.gz (25K) Download Attachment