forked from TomWhitwell/SlowMovie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelloworld.py
More file actions
executable file
·84 lines (65 loc) · 2.57 KB
/
Copy pathhelloworld.py
File metadata and controls
executable file
·84 lines (65 loc) · 2.57 KB
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
#!/usr/bin/python
# -*- coding:utf-8 -*-
# *************************
# ** Before running this **
# ** code ensure you've **
# ** turned on SPI on **
# ** your Raspberry Pi **
# ** & installed the **
# ** Waveshare library **
# *************************
import os, time, sys, random
from PIL import Image
import ffmpeg
def generate_frame(in_filename, out_filename, time, width, height):
(
ffmpeg
.input(in_filename, ss=time)
.filter('scale', width, height, force_original_aspect_ratio=1)
.filter('pad', width, height, -1, -1)
.output(out_filename, vframes=1)
.overwrite_output()
.run(capture_stdout=True, capture_stderr=True)
)
# Ensure this is the correct import for your particular screen
from waveshare_epd import epd7in5_V2
# Ensure this is the correct path to your video folder
viddir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'Videos/')
# Ensure this is the correct driver for your particular screen
epd = epd7in5_V2.EPD()
# Initialise and clear the screen
epd.init()
epd.Clear()
while 1:
# Pick a random .mp4 video in your video directory
currentVideo = ""
while not (currentVideo.endswith('.mp4')):
videoCount = len(os.listdir(viddir))
randomVideo = random.randint(0,videoCount-1)
currentVideo = os.listdir(viddir)[randomVideo]
inputVid = viddir + currentVideo
print(inputVid)
# Ensure this matches your particular screen
width = 800
height = 480
# Check how many frames are in the movie
frameCount = int(ffmpeg.probe(inputVid)['streams'][0]['nb_frames'])
# Pick a random frame
frame = random.randint(0,frameCount)
# Convert that frame to Timecode
msTimecode = "%dms"%(frame*41.666666)
# Use ffmpeg to extract a frame from the movie, crop it, letterbox it and save it as grab.jpg
generate_frame(inputVid, 'grab.jpg', msTimecode, width, height)
# Open grab.jpg in PIL
pil_im = Image.open("grab.jpg")
# Dither the image into a 1 bit bitmap (Just zeros and ones)
pil_im = pil_im.convert(mode='1',dither=Image.FLOYDSTEINBERG)
# display the image
epd.display(epd.getbuffer(pil_im))
print('Diplaying frame %d of %s' %(frame,currentVideo))
# Wait for 10 seconds
time.sleep(10)
# NB We should run sleep() while the display is resting more often, but there's a bug in the driver that's slightly fiddly to fix. Instead of just sleeping, it completely shuts down SPI communication
epd.sleep()
epd7in5.epdconfig.module_exit()
exit()