Class: FFruby::File

Inherits:
Object
  • Object
show all
Defined in:
ext/ffruby/ffrubyfile.c,
ext/ffruby/ffrubyfile.c

Overview

An interface to FFmpeg on existing files. Provides access to metadata and stream instances.

Instance Method Summary collapse

Constructor Details

#new(filename) ⇒ FFruby::File

Creates an FFruby::File instance using the given filename.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'ext/ffruby/ffrubyfile.c', line 207

static VALUE ffrf_initialize(VALUE self, VALUE filename)
{
	size_t len;
	char* msg;
	AVFormatContext *fmt;
	VALUE exception;

	VALUE filename_str = rb_funcall(filename, rb_intern("to_s"), 0);
    char* filename_ptr = RSTRING(filename_str)->ptr;

	if (av_open_input_file(&fmt, filename_ptr, NULL, 0, NULL) != 0)
	{
		len = strlen("Cannot open file !") + strlen(filename_ptr) + 1;
		msg = ALLOC_N(char, len);
		snprintf(msg, len, "Cannot open file %s!", filename_ptr);
		exception = rb_exc_new2(rb_eIOError, msg);
		free(msg);
		rb_exc_raise(exception);
	}

	if (av_find_stream_info(fmt) < 0)
	{
		len = strlen("Problem reading file !") + strlen(filename_ptr) + 1;
		msg = ALLOC_N(char, len);
		snprintf(msg, len, "Problem reading file %s!", filename_ptr);
		exception = rb_exc_new2(rb_eIOError, msg);
		free(msg);
		rb_exc_raise(exception);
	}

	DATA_PTR(self) = fmt;
	return self;
}

Instance Method Details

#albumObject

Returns the album string.



85
86
87
88
89
# File 'ext/ffruby/ffrubyfile.c', line 85

static VALUE ffrf_album(VALUE self)
{
	AVFormatContext *fmt = ffrf_get_fmt(self);
	return rb_str_new2(fmt->album);
}

#audio_streamsObject

Returns an array of audio streams contained within this file.



198
199
200
201
# File 'ext/ffruby/ffrubyfile.c', line 198

static VALUE ffrf_audio_streams(VALUE self)
{
	return ffrf_av_streams(self, cFFrubyAudioStream);
}

#authorObject

Returns the author string.



64
65
66
67
68
# File 'ext/ffruby/ffrubyfile.c', line 64

static VALUE ffrf_author(VALUE self)
{
	AVFormatContext *fmt = ffrf_get_fmt(self);
	return rb_str_new2(fmt->author);
}

#bit_rateObject

Returns the bit rate.



120
121
122
123
124
# File 'ext/ffruby/ffrubyfile.c', line 120

static VALUE ffrf_bit_rate(VALUE self)
{
	AVFormatContext *fmt = ffrf_get_fmt(self);
	return INT2NUM(fmt->bit_rate);
}

#commentObject

Returns the comment string.



78
79
80
81
82
# File 'ext/ffruby/ffrubyfile.c', line 78

static VALUE ffrf_comment(VALUE self)
{
	AVFormatContext *fmt = ffrf_get_fmt(self);
	return rb_str_new2(fmt->comment);
}

Returns the copyright string.



71
72
73
74
75
# File 'ext/ffruby/ffrubyfile.c', line 71

static VALUE ffrf_copyright(VALUE self)
{
	AVFormatContext *fmt = ffrf_get_fmt(self);
	return rb_str_new2(fmt->copyright);
}

#durationObject

Returns the duration in seconds as a float.



113
114
115
116
117
# File 'ext/ffruby/ffrubyfile.c', line 113

static VALUE ffrf_duration(VALUE self)
{
	AVFormatContext *fmt = ffrf_get_fmt(self);
	return rb_float_new((double) fmt->duration / (double) AV_TIME_BASE);
}

#formatObject

Returns the format name.



127
128
129
130
131
# File 'ext/ffruby/ffrubyfile.c', line 127

static VALUE ffrf_format(VALUE self)
{
	AVFormatContext *fmt = ffrf_get_fmt(self);
	return rb_str_new2(fmt->iformat->name);
}

#genreObject

Returns the genre string.



92
93
94
95
96
# File 'ext/ffruby/ffrubyfile.c', line 92

static VALUE ffrf_genre(VALUE self)
{
	AVFormatContext *fmt = ffrf_get_fmt(self);
	return rb_str_new2(fmt->genre);
}

#streamsObject

Returns an array of streams contained within this file.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'ext/ffruby/ffrubyfile.c', line 134

static VALUE ffrf_streams(VALUE self)
{
	unsigned int i;
	AVFormatContext *fmt;
	VALUE streams;
	VALUE* args;

	if ((streams = rb_iv_get(self, "@streams")) == Qnil)
	{
		fmt = ffrf_get_fmt(self);
		streams = rb_ary_new();
		rb_iv_set(self, "@streams", streams);

		args = (VALUE*) ALLOCA_N(VALUE*, 2);
		args[0] = self;

		for (i = 0; i < fmt->nb_streams; i++)
		{
			args[1] = INT2FIX(i);

			switch (fmt->streams[i]->codec->codec_type)
			{
				case CODEC_TYPE_VIDEO:
					rb_ary_push(streams, rb_class_new_instance(2, args, cFFrubyVideoStream));
					break;
				case CODEC_TYPE_AUDIO:
					rb_ary_push(streams, rb_class_new_instance(2, args, cFFrubyAudioStream));
					break;
				default:
					break;
			}
		}
	}

	return streams;
}

#titleObject

Returns the title string.



57
58
59
60
61
# File 'ext/ffruby/ffrubyfile.c', line 57

static VALUE ffrf_title(VALUE self)
{
	AVFormatContext *fmt = ffrf_get_fmt(self);
	return rb_str_new2(fmt->title);
}

#trackObject

Returns the track number.



106
107
108
109
110
# File 'ext/ffruby/ffrubyfile.c', line 106

static VALUE ffrf_track(VALUE self)
{
	AVFormatContext *fmt = ffrf_get_fmt(self);
	return INT2NUM(fmt->track);
}

#video_streamsObject

Returns an array of video streams contained within this file.



192
193
194
195
# File 'ext/ffruby/ffrubyfile.c', line 192

static VALUE ffrf_video_streams(VALUE self)
{
	return ffrf_av_streams(self, cFFrubyVideoStream);
}

#yearObject

Returns the year.



99
100
101
102
103
# File 'ext/ffruby/ffrubyfile.c', line 99

static VALUE ffrf_year(VALUE self)
{
	AVFormatContext *fmt = ffrf_get_fmt(self);
	return INT2NUM(fmt->year);
}