File: video.py

package info (click to toggle)
pigment-python 0.3.12-2.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,820 kB
  • ctags: 2,955
  • sloc: python: 11,664; sh: 10,131; makefile: 250; ansic: 97
file content (132 lines) | stat: -rw-r--r-- 4,297 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- mode: python; coding: utf-8 -*-
#
# Pigment Python binding video example
#
# Copyright © 2006, 2007, 2008 Fluendo Embedded S.L.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# Author: Loïc Molinari <loic@fluendo.com>

import sys, pgm, gobject, gst

fps_label = None

# Key press handler
def on_key_press(viewport, event, pipeline):
    # Terminate the mainloop on 'Escape' or 'q' key press event
    if event.keyval == pgm.keysyms.q or event.keyval == pgm.keysyms.Escape:
        pgm.main_quit()
    # Switch fullscreen state
    if event.keyval == pgm.keysyms.f:
        viewport.fullscreen = not viewport.fullscreen
    # Toggle fps display
    elif event.keyval == pgm.keysyms.p:
        global fps_label
        canvas = viewport.get_canvas()
        if fps_label == None:
            add_fps_display(canvas, viewport)
        else:
            canvas.remove(fps_label)
            fps_label = None

# Drag motion handler
def on_drag_motion(viewport, event, pipeline):
    # Accept the drag if the extension of the first uri is a media file
    if event.uri[0][-3:] in ("avi", "mpg", "ogg", "mkv", "mov", "wmv", "flv"):
        return True

# Drag drop handler
def on_drag_drop(viewport, event, pipeline):
    pipeline.set_state(gst.STATE_READY)
    pipeline.set_property('uri', event.uri[0])
    pipeline.set_state(gst.STATE_PLAYING)

# Terminate the mainloop on a delete event
def on_delete(viewport, event, pipeline):
    pipeline.set_state(gst.STATE_NULL)
    pgm.main_quit()

def add_fps_display(canvas, viewport):
    global fps_label
    fps_label = pgm.Text()
    fps_label.bg_a = 0
    fps_label.width = canvas.width/10.0
    fps_label.height = canvas.height/20.0
    fps_label.x = canvas.width - fps_label.width
    fps_label.y = canvas.height - fps_label.height
    fps_label.visible = True
    canvas.add(pgm.DRAWABLE_NEAR, fps_label)
    refresh_fps_display(viewport)
    gobject.timeout_add(1000, refresh_fps_display, viewport)

def refresh_fps_display(viewport):
    global fps_label
    if fps_label != None:
        fps_label.label = "%d fps" % viewport.frame_rate
        return True
    else:
        return False

def main(args):
    # GObject threads initialization
    gobject.threads_init()

    # OpenGL viewport creation
    gl = pgm.viewport_factory_make('opengl')
    gl.title = "Video drag 'n drop"
    gl.alpha_blending = False
    screen_ratio = float(gl.screen_size_mm[1])/gl.screen_size_mm[0]
    gl.size = (800, 800*screen_ratio)

    # Canvas and image drawable creation
    cvs = pgm.Canvas()
    cvs.size = (1.0, 1.0*screen_ratio)
    img = pgm.Image()

    # Bind the canvas to the OpenGL viewport
    gl.set_canvas(cvs)

    # Image properties
    img.position = (0.0, 0.0, 0.0)
    img.size = cvs.size
    img.bg_color = (0, 0, 0, 0)

    # A drawable needs to be shown
    img.show()

    # Add it to the middle layer of the canvas
    cvs.add(pgm.DRAWABLE_MIDDLE, img)

    # GStreamer pipeline setup
    sink = gst.element_factory_make('pgmimagesink')
    pipeline = gst.element_factory_make('playbin')
    pipeline.set_property('video-sink', sink)
    sink.set_property('image', img)
    if (len(sys.argv) > 1):
        pipeline.set_property('uri', sys.argv[1])
        pipeline.set_state(gst.STATE_PLAYING)

    # Let's start the mainloop
    gl.connect('delete-event', on_delete, pipeline)
    gl.connect('key-press-event', on_key_press, pipeline)
    gl.connect('drag-motion-event', on_drag_motion, pipeline)
    gl.connect('drag-drop-event', on_drag_drop, pipeline)
    gl.show()
    pgm.main()

if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))