Created
September 15, 2015 02:17
-
-
Save andrewrk/ad619e93727a63d676b3 to your computer and use it in GitHub Desktop.
gcc -o test test.c -lavformat && ./test ~/Music/eddie-money-home-tonight.m4a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <libavcodec/avcodec.h> | |
#include <libavfilter/avfilter.h> | |
#include <libavfilter/buffersink.h> | |
#include <libavfilter/buffersrc.h> | |
#include <libavformat/avformat.h> | |
#include <libavformat/avio.h> | |
#include <libavutil/channel_layout.h> | |
#include <libavutil/dict.h> | |
#include <libavutil/frame.h> | |
#include <libavutil/log.h> | |
#include <libavutil/mem.h> | |
#include <libavutil/opt.h> | |
struct Context { | |
FILE *file; | |
}; | |
static void panic(char *msg) { | |
fprintf(stderr, "%s\n", msg); | |
abort(); | |
} | |
static int read_packet(void *opaque, uint8_t *buf, int buf_size) { | |
struct Context *context = opaque; | |
return fread(buf, 1, buf_size, context->file); | |
} | |
static int write_packet(void *opaque, uint8_t *buf, int buf_size) { | |
struct Context *context = opaque; | |
return fwrite(buf, 1, buf_size, context->file); | |
} | |
static int64_t seek(void *opaque, int64_t offset, int whence) { | |
struct Context *context = opaque; | |
return fseek(context->file, offset, whence); | |
} | |
static int decode_interrupt_cb(void *ctx) { | |
return 0; | |
} | |
int main(int argc, char **argv) { | |
struct Context context; | |
avcodec_register_all(); | |
av_register_all(); | |
char *filename = argv[1]; | |
context.file = fopen(filename, "rb"); | |
if (!context.file) | |
panic("unable to open file"); | |
AVFormatContext *ic = avformat_alloc_context(); | |
if (!ic) | |
panic("unable to alloc ic"); | |
ic->interrupt_callback.callback = decode_interrupt_cb; | |
ic->interrupt_callback.opaque = &context; | |
static const int buffer_size = 8 * 1024; | |
unsigned char *avio_buf = av_malloc(buffer_size); | |
if (!avio_buf) | |
panic("unable to alloc avio_buf"); | |
AVIOContext *avio = avio_alloc_context(avio_buf, buffer_size, 0, &context, | |
read_packet, write_packet, seek); | |
if (!avio) | |
panic("unable to alloc avio context"); | |
// comment this line out and it will work | |
ic->pb = avio; | |
int err = avformat_open_input(&ic, filename, NULL, NULL); | |
if (err < 0) { | |
char buf[512]; | |
av_strerror(err, buf, sizeof(buf)); | |
fprintf(stderr, "open error: '%s'\n", buf); | |
panic("it failed"); | |
} | |
fprintf(stderr, "OK\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment