Skip to content

Instantly share code, notes, and snippets.

/ruby.rb Secret

Created December 5, 2015 04:41
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 anonymous/daeeb7e9514895c2c69e to your computer and use it in GitHub Desktop.
Save anonymous/daeeb7e9514895c2c69e 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_mark(void *p){
};
void r_avformat_context_free(void *p) {
AVFormatContext *ctx = (AVFormatContext *) p;
if (ctx) {
avformat_free_context(ctx);
}
};
VALUE r_avformat_open_input_file(VALUE self, VALUE context, VALUE filename) {
AVFormatContext *ctx;
char* fn;
int res = -1;
Check_Type(filename, T_STRING);
Data_Get_Struct(context, AVFormatContext, ctx);
fn = StringValuePtr(filename);
res = avformat_open_input(&ctx, fn, NULL, NULL);
return INT2NUM(res);
};
VALUE r_avformat_close_input_file(VALUE self, VALUE context) {
AVFormatContext *ctx;
Data_Get_Struct(context, AVFormatContext, ctx);
avformat_close_input(&ctx);
ctx = NULL;
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, 2);
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