Created
July 1, 2015 10:50
-
-
Save aborilov/812e781066060a8466ee to your computer and use it in GitHub Desktop.
video snapshots with gstreamer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding: utf-8 | |
| import os | |
| import sys | |
| from gi.repository import Gst | |
| SEC = 1 | |
| Gst.init(None) | |
| caps = Gst.Caps.from_string('image/jpeg') | |
| pipeline = Gst.parse_launch('playbin') | |
| path = os.path.abspath(sys.argv[1]) | |
| pipeline.set_property('uri', 'file://{}'.format(path)) | |
| audio_sink = Gst.ElementFactory.make('fakesink', 'audiosink') | |
| video_sink = Gst.ElementFactory.make('fakesink', 'videosink') | |
| pipeline.set_property('audio-sink', audio_sink) | |
| pipeline.set_property('video-sink', video_sink) | |
| pipeline.set_state(Gst.State.PAUSED) | |
| pipeline.get_state(Gst.SECOND) | |
| duration = pipeline.query_duration(Gst.Format.TIME)[1]/Gst.SECOND | |
| for i in range(0, duration/SEC): | |
| assert pipeline.seek_simple( | |
| Gst.Format.TIME, Gst.SeekFlags.FLUSH, i*SEC*Gst.SECOND) | |
| pipeline.get_state(Gst.SECOND) | |
| sample = pipeline.emit('convert-sample', caps) | |
| buffer = sample.get_buffer() | |
| image = buffer.extract_dup(0, buffer.get_size()) | |
| with open('JPEG/frame{}.jpeg'.format(i), 'wb') as f: | |
| f.write(image) | |
| pipeline.set_state(Gst.State.NULL) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment