Skip to content

Instantly share code, notes, and snippets.

@thapar
Created March 18, 2013 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thapar/5191486 to your computer and use it in GitHub Desktop.
Save thapar/5191486 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#import pygst
#pygst.require('0.10')
from gi.repository import Gtk
Gtk.init(None)
from gi.repository import Gst, GstVideo
Gst.init(None)
#import pygtk
#import gtk
#from PIL import Image
#from repoze.filesafe import create_file
#import scaler
from perfect_thumb import PerfectThumb
import sys
import datetime
import pdb
def snapshot(source_dir, destination_dir, num_snaps, max_width=100):
# ## Debugger ###############################################
Gst.debug_set_default_threshold(Gst.DebugLevel.LOG)
# Gst.debug_set_threshold_for_name('gdkpixbufsink', Gst.DebugLevel.LOG)
# ##########################################################
## ## OLD #######
# CAPS = "video/x-raw-rgb,pixel-aspect-ratio=1/1,bpp=(int)24,depth=(int)24,endianness=(int)4321,red_mask=(int)0xff0000, green_mask=(int)0x00ff00, blue_mask=(int)0x0000ff"
# descr = 'uridecodebin uri=%s ! ffmpegcolorspace ! videoscale ! appsink name=sink caps="%s"' % (source_path, CAPS)
# CAPS = "video/x-raw,format=RGB,pixel-aspect-ratio=1/1"
#####################################################################
## description of pipeline
descr = 'uridecodebin uri=%s ! videoconvert ! videoscale ! gdkpixbufsink name=sink' % (source_dir)
## pipeline created
pipeline = Gst.parse_launch(descr)
## getting part of pipeline
sink = pipeline.get_by_name('sink')
##############################################################
## set to PAUSED to make the first frame arrive in the sink
ret = pipeline.set_state(Gst.State.PAUSED)
print 'ret: ', ret
## error check
if ret == Gst.StateChangeReturn.FAILURE:
print 'failed to play the file (1)\n'
elif ret == Gst.StateChangeReturn.NO_PREROLL:
print 'live sources not supported yet\n'
############################################
## getting the element's state (protected with a timeout)
ret = pipeline.get_state(5 * Gst.SECOND)
## error check
if ret[0] == Gst.StateChangeReturn.FAILURE:
print 'failed to play the file (2)\n'
sys.exit()
#############################################
## getting the duration
format = Gst.Format.TIME ## choices besides TIME to put thru query_duration?
duration = pipeline.query_duration(format)[1]
# pdb.set_trace()
pixbuf = sink.props.last_pixbuf
h = pixbuf.get_height()
w = pixbuf.get_width()
rowstride = w*3 + (w*3 % 4)
##############################################
##
for snap in xrange(num_snaps):
if duration > 0:
position = duration * (snap * 0.05) ## to go 5% into the video
else:
position = 1 * Gst.SECOND ## seek to 1 second (this could cause EOS)
print position
# pipeline.seek_simple(Gst.Format.TIME,
# Gst.SeekFlags.KEY_UNIT | Gst.SeekFlags.FLUSH,
# position)
ret = pipeline.seek_simple(Gst.Format.TIME,
Gst.SeekFlags.KEY_UNIT | Gst.SeekFlags.FLUSH,
position)
## getting the element's state (protected with a timeout)
ret = pipeline.get_state(5 * Gst.SECOND)
## error check
if not ret:
print 'failed to seek forward\n'
sys.exit()
if ret == Gst.StateChangeReturn.FAILURE:
print 'failed to play the file (1)\n'
sys.exit()
elif ret == Gst.StateChangeReturn.NO_PREROLL:
print 'live sources not supported yet\n'
pixbuf = sink.props.last_pixbuf
################################
## choosing a desired path/to/filename ####
filename = 'snapshot-' + str(snap) + '.png'
dest_dir = destination_dir
dest_dir = dest_dir if dest_dir[-1] == '/' else dest_dir + '/'
destination_path = dest_dir + filename
## shrink it and save it
PerfectThumb.shrink_raw(pixbuf.get_pixels(),
destination_path,
w, h, rowstride,
xy=max_width)
# pixbuf.save('snapshot.png', 'png')
pipeline.set_state(Gst.State.NULL)
sys.exit()
def get_filename(filename, itr=None, ext='.png'):
return filename + ext
thumb_path = '/home/raj/vbox_shared'
source_path = 'file:///home/raj/vbox_shared/sample.avi'
start = snapshot(source_path, thumb_path, 3)
#gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment