Created
January 27, 2016 18:26
-
-
Save sorz/3b49d10029747472439a to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python3 | |
import sys | |
from videoseeker import VideoSeeker | |
def main(): | |
seeker = VideoSeeker(sys.argv[1]) | |
for i in range(100): | |
seeker.seek() | |
seeker.get_image().save('output/test-%03d.jpg' % i) | |
#seeker.seek() | |
#for i, image in enumerate(seeker.get_images(10)): | |
# print(i) | |
# image.save('output/test-%03d.jpg' % i) | |
if __name__ == '__main__': | |
main() |
This file contains 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
#!/usr/bin/env python3 | |
from random import SystemRandom | |
import av | |
class VideoSeeker: | |
def __init__(self, path): | |
self.path = path | |
self._container = av.open(path) | |
self._video = next(s for s in self._container.streams | |
if s.type == 'video') | |
self._random = SystemRandom() | |
def seek(self, pos=None): | |
if pos is None: | |
pos = self._random.random() | |
elif pos < 0 or pos > 1: | |
raise ValueError('pos must be a float between 0 and 1.') | |
self._video.seek(round(self._video.duration * pos), any_frame=True) | |
def get_images(self, interval=0): | |
packets = self._container.demux(self._video) | |
frame_count = 0 | |
for packet in packets: | |
for frame in packet.decode(): | |
frame_count += 1 | |
if frame_count % (interval + 1) == 0: | |
yield frame.to_image() | |
def get_image(self): | |
return next(self.get_images()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment