Skip to content

Instantly share code, notes, and snippets.

@belovachap
Created February 27, 2012 05:02
Show Gist options
  • Save belovachap/1921521 to your computer and use it in GitHub Desktop.
Save belovachap/1921521 to your computer and use it in GitHub Desktop.
Resize video width/height with GStreamer and Vala
// Scales the video stream from an ogg video file
using Gst;
public class VideoScaler {
private MainLoop loop = new MainLoop ();
private Pipeline pipeline;
private bool bus_callback (Bus bus, Message message) {
switch (message.type) {
case MessageType.ERROR:
GLib.Error err;
string debug;
message.parse_error (out err, out debug);
stdout.printf ("Error: %s\n", err.message);
loop.quit ();
break;
case MessageType.EOS:
stdout.printf("End of stream reached\n");
loop.quit ();
break;
default:
break;
}
return true;
}
public void resize (string file_name, int width, int height) {
pipeline = new Pipeline ("resize_video");
var file_src = ElementFactory.make ("filesrc", "file_input");
file_src.set ("location", file_name);
var demuxer = ElementFactory.make ("oggdemux", "demuxer");
var decoder = ElementFactory.make ("theoradec", "decoder");
var scaler = ElementFactory.make ("videoscale", "scaler");
var filter = ElementFactory.make ("capsfilter", "filter");
var video_caps = new Caps.simple("video/x-raw-yuv",
"width", typeof(int), width,
"height", typeof(int), height);
filter.set ("caps", video_caps);
var encoder = ElementFactory.make ("theoraenc", "encoder");
encoder.set ("quality", 63);
var muxer = ElementFactory.make ("oggmux", "muxer");
var file_sink = ElementFactory.make ("filesink", "file_sink");
file_sink.set ("location", "out.ogv");
pipeline.add_many (file_src,
demuxer,
decoder,
scaler,
filter,
encoder,
muxer,
file_sink);
file_src.link (demuxer);
// demuxer has automatic srcs created at runtime that we need to
// setup a callback to link
decoder.link (scaler);
scaler.link (filter);
filter.link (encoder);
encoder.link (muxer);
muxer.link (file_sink);
// Add handler for the demuxer
demuxer.pad_added.connect(demux_added_pad);
Bus bus = pipeline.get_bus ();
bus.add_watch (bus_callback);
pipeline.set_state (State.PLAYING);
loop.run ();
}
private void demux_added_pad(Pad new_pad) {
// See if we can link this pad any of the muxer pads
var decoder = pipeline.get_by_name ("decoder");
foreach (Pad sink_pad in decoder.sinkpads) {
if (!sink_pad.is_linked () && new_pad.can_link (sink_pad)) {
new_pad.link (sink_pad);
return;
}
}
// Wasn't able to link the pad? Remove it.
var demuxer = pipeline.get_by_name("demuxer");
demuxer.remove_pad (new_pad);
}
}
int main (string[] args) {
if (args.length != 4) {
stdout.printf("Usage: %s <ogg video file> <width> <height>\n", args[0]);
return -1;
}
Gst.init (ref args);
var scaler = new VideoScaler ();
scaler.resize(args[1], int.parse(args[2]), int.parse(args[3]));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment