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.



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
240
241
242
243
# File 'ext/ffruby/ffrubyfile.c', line 211

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.



89
90
91
92
93
# File 'ext/ffruby/ffrubyfile.c', line 89

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.



202
203
204
205
# File 'ext/ffruby/ffrubyfile.c', line 202

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

#authorObject

Returns the author string.



68
69
70
71
72
# File 'ext/ffruby/ffrubyfile.c', line 68

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

#bit_rateObject

Returns the bit rate.



124
125
126
127
128
# File 'ext/ffruby/ffrubyfile.c', line 124

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

#commentObject

Returns the comment string.



82
83
84
85
86
# File 'ext/ffruby/ffrubyfile.c', line 82

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

Returns the copyright string.



75
76
77
78
79
# File 'ext/ffruby/ffrubyfile.c', line 75

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.



117
118
119
120
121
# File 'ext/ffruby/ffrubyfile.c', line 117

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.



131
132
133
134
135
# File 'ext/ffruby/ffrubyfile.c', line 131

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

#genreObject

Returns the genre string.



96
97
98
99
100
# File 'ext/ffruby/ffrubyfile.c', line 96

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.



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
170
171
172
173
# File 'ext/ffruby/ffrubyfile.c', line 138

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.



61
62
63
64
65
# File 'ext/ffruby/ffrubyfile.c', line 61

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

#trackObject

Returns the track number.



110
111
112
113
114
# File 'ext/ffruby/ffrubyfile.c', line 110

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.



196
197
198
199
# File 'ext/ffruby/ffrubyfile.c', line 196

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

#yearObject

Returns the year.



103
104
105
106
107
# File 'ext/ffruby/ffrubyfile.c', line 103

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