Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

potyl (8582)

potyl
  (email not shown publicly)
Jabber: emmanuel.rodriguez@gmail.com

Journal of potyl (8582)

Wednesday July 01, 2009
03:38 PM

Perl movie player

[ #39204 ]

A few months ago I was playing with Gstreamer and I wrote a small movie player in Perl 5 thanks to the Gstreamer bindings.

For those that aren't familiar with Gstreamer it consists of an open source multimedia framework. Gstreamer allows an application to play, stream and covert any kind of multi-media content. It is mainly used in the Gnome desktop for playing sounds and viewing movies.

If you are a bit curious play with the following movie player:

#!/usr/bin/perl

=head1 NAME

gst-video-player.pl - Video player made in Perl

=head1 SYNOPSIS

gst-video-player.pl video

Where I<video> is the URI to a video (file, http, etc).

=head1 DESCRIPTION

This program shows how to create a video player using Gtk2 and Gstreamer. This
player can handle all video formats supported by Gstreamer.

=cut

use strict;
use warnings;

use Glib qw(TRUE FALSE filename_to_uri);
use GStreamer '-init';
use GStreamer::Interfaces;
use Gtk2 '-init';
use File::Spec;
use Cwd;

exit main();

sub main {
    die "Usage: file\n" unless @ARGV;
    my ($uri) = @ARGV;

    if ($uri =~ m,^[^:]+://,) {
        # Nothing to do as the input is already an URI
    }
    elsif (! File::Spec->file_name_is_absolute($uri)) {
        my $file = File::Spec->catfile(getcwd(), $uri);
        $uri = filename_to_uri($file, undef);
    }
    else {
        $uri = filename_to_uri($uri, undef);
    }
    print "Playing: $uri\n";

    # Create the main pipeline and GUI elements
    my ($pipeline, $player, $sink) = create_pipeline();
    my ($window, $canvas, $buttons) = create_widgets();

    $player->set(uri => $uri);

    # Buttons used to control the playback
    add_button($buttons, 'gtk-media-play', sub {
        $sink->set_xwindow_id($canvas->window->get_xid);
        $pipeline->set_state('playing');
    });

    add_button($buttons, 'gtk-media-stop', sub {
        $pipeline->set_state('ready');
    });

    # Run the program
    Gtk2->main();

    # Cleanup
    $pipeline->set_state('null');
    return 0;
}

sub create_pipeline {
    my $pipeline = GStreamer::Pipeline->new('pipeline');

    # The pipeline elements
    my ($player, $sink) = GStreamer::ElementFactory->make(
        playbin     => 'player',
        xvimagesink => 'sink',
    );

    $pipeline->add($player);
    $player->link($sink);

    $player->set('video-sink', $sink);
    $sink->set('force-aspect-ratio', TRUE);

    return ($pipeline, $player, $sink);
}

sub create_widgets {
    # Create the widgets
    my $window = Gtk2::Window->new();
    $window->set_title("Gst video test");

    # This is where the video will be displayed
    my $canvas = Gtk2::DrawingArea->new();
    $canvas->set_size_request(300, 150);

    my $vbox = Gtk2::VBox->new(FALSE, 0);
    $vbox->pack_start($canvas, TRUE, TRUE, 0);

    # Prepare a box that will hold the playback controls
    my $buttons = Gtk2::HButtonBox->new();
    $vbox->pack_start($buttons, FALSE, TRUE, 0);

    $window->add($vbox);

    $window->signal_connect(destroy => sub {Gtk2->main_quit()});
    $window->show_all();

    return ($window, $canvas, $buttons);
}

sub add_button {
    my ($box, $stock, $callback) = @_;
    my $button = Gtk2::Button->new_from_stock($stock);
    $button->signal_connect(clicked => $callback);
    $box->add($button);
    $button->show_all();
}

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • How do you get GStreamer::Interfaces built? I'm always left with a blank Makefile after the perl Makefile.PL stage.
    • I've built it as a Deb package (Ubuntu 9.04) and installed it that way. If you are using that same version of ubuntu you can use my repository:

      deb http://debian.potyl.com/ jaunty main

      Once you have enabled it you can install the library with:

      sudo apt-get udpate && sudo apt-get install libgstreamer-interfaces-perl

      If that doesn't work paste your error messages here and I will try to help. Keep in mind that in order to build GStreamer::Interfaces you will need to have all build time dependencies in