forked from TomWhitwell/SlowMovie
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelloworld.py
More file actions
executable file
·109 lines (85 loc) · 3.01 KB
/
helloworld.py
File metadata and controls
executable file
·109 lines (85 loc) · 3.01 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
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
#!/usr/bin/python3
# -*- 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, signal
from PIL import Image, ImageEnhance
import ffmpeg
from fractions import Fraction
# Ensure this is the correct import for your particular screen
from waveshare_epd import epd7in5_V2 as epd_driver
fileTypes = [".mp4", ".mkv"]
def exithandler(signum, frame):
try:
epd_driver.epdconfig.module_exit()
finally:
sys.exit()
signal.signal(signal.SIGTERM, exithandler)
signal.signal(signal.SIGINT, exithandler)
def generate_frame(in_filename, out_filename, time):
(
ffmpeg
.input(in_filename, ss=time)
.filter("scale", "iw*sar", "ih")
.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)
)
def supported_filetype(file):
_, ext = os.path.splitext(file)
return ext.lower() in fileTypes
# Ensure this is the correct path to your video folder
viddir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "Videos")
if not os.path.isdir(viddir):
os.mkdir(viddir)
# Pick a random .mp4 video in your video directory
videos = list(filter(supported_filetype, os.listdir(viddir)))
if not videos:
print("No videos found")
sys.exit()
epd = epd_driver.EPD()
width = epd.width
height = epd.height
currentVideo = None
videoInfos = {}
while 1:
epd.init()
lastVideo = currentVideo
currentVideo = os.path.join(viddir, random.choice(videos))
if lastVideo != currentVideo:
# Check how many frames are in the movie
if currentVideo in videoInfos:
videoInfo = videoInfos[currentVideo]
else:
videoInfo = ffmpeg.probe(currentVideo)
videoInfos[currentVideo] = videoInfo
frameCount = int(videoInfo["streams"][0]["nb_frames"])
framerate = videoInfo["streams"][0]["avg_frame_rate"]
framerate = float(Fraction(framerate))
frametime = 1000 / framerate
# Pick a random frame
frame = random.randint(0, frameCount)
# Convert that frame to Timecode
msTimecode = "%dms" % (frame * frametime)
# Use ffmpeg to extract a frame from the movie, letterbox/pillarbox, and save it
generate_frame(currentVideo, "/dev/shm/frame.bmp", msTimecode)
# Open image in PIL
pil_im = Image.open("/dev/shm/frame.bmp")
enhancer = ImageEnhance.Contrast(pil_im)
pil_im = enhancer.enhance(2)
# Dither the image into a 1 bit bitmap
#pil_im = pil_im.convert(mode = "1", dither = Image.FLOYDSTEINBERG)
# display the image
print("Displaying frame %d of %s" % (frame, os.path.basename(currentVideo)))
epd.display(epd.getbuffer(pil_im))
# Wait for 10 seconds
epd.sleep()
time.sleep(10)