Skip to content

Instantly share code, notes, and snippets.

@sh4d0wPhoenix
Created December 5, 2015 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sh4d0wPhoenix/95fd83be24904a537bb6 to your computer and use it in GitHub Desktop.
Save sh4d0wPhoenix/95fd83be24904a537bb6 to your computer and use it in GitHub Desktop.
#include "ffmpeg4r_avformat.h"
VALUE r_av_register_all( void ) {
av_register_all();
return Qnil;
};
VALUE r_avformat_context_allocate(VALUE klass) {
AVFormatContext *ctx = avformat_alloc_context();
return Data_Wrap_Struct(klass, NULL, r_avformat_context_free, ctx);
}
;
void r_avformat_context_free(AVFormatContext *p) {
if (!p) return;
if (p->iformat != NULL) {
avformat_close_input(&p);
return;
}
};
VALUE r_avformat_open_input_file(VALUE self, VALUE filename) {
AVFormatContext *ctx;
char* fn;
int res = 1;
VALUE obj = rb_obj_alloc(cAVFormatContext);
Check_Type(filename, T_STRING);
fn = StringValuePtr(filename);
rb_obj_call_init(obj, 0, NULL);
Data_Get_Struct(obj, AVFormatContext, ctx);
res = avformat_open_input(&ctx, fn, NULL, NULL);
if (res != 0) {
rb_raise(rb_eStandardError, "Could not open file");
return Qnil;
}
return obj;
};
VALUE r_avformat_close_input_file(VALUE self, VALUE context) {
AVFormatContext *ctx;
Data_Get_Struct(context, AVFormatContext, ctx);
avformat_close_input(&ctx);
return Qnil;
};
VALUE r_avformat_context_metadata(VALUE self) {
AVFormatContext *ctx;
AVDictionaryEntry *t = NULL;
VALUE result = rb_hash_new();
Data_Get_Struct(self, AVFormatContext, ctx);
while (t = av_dict_get(ctx->metadata, "", t, AV_DICT_IGNORE_SUFFIX)) {
rb_hash_aset(result, rb_str_new2(t->key), rb_str_new2(t->value));
}
return result;
};
VALUE r_avformat_context_initialize(VALUE self) {
return self;
}
void Init_ffmpeg4r_avformat(VALUE module) {
mAVFormat = rb_define_module_under(module, "AVFormat");
rb_define_module_function(mAVFormat, "av_register_all", r_av_register_all, 0);
rb_define_module_function(mAVFormat, "avformat_open_input_file", r_avformat_open_input_file, 1);
rb_define_module_function(mAVFormat, "avformat_close_input_file", r_avformat_close_input_file, 1);
// FFmpeg4r::AVFormat::AVFormatContext
cAVFormatContext = rb_define_class_under(mAVFormat, "AVFormatContext", rb_cObject);
rb_define_alloc_func(cAVFormatContext, r_avformat_context_allocate);
rb_define_method(cAVFormatContext, "initialize", r_avformat_context_initialize, 0);
rb_define_method(cAVFormatContext, "metadata", r_avformat_context_metadata, 0);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment