I was working on a Gtk 3 project to roll up all my iPhone videos into a flowbox for easy locating and processing since iPhone doesn’t name things in a meaningful way and photo managers dump things into not so meaningful folders, thumbnails was a must to locate videos easily. Plus, I wanted to do some Gtk programming.
I started with GStreamer 1.0, which worked, but not for .mov files. This handles MP4 and MOV. I have not tested other types so it might need further tweaks.
Hope it is helpful to someone. I can provide GStreamer code if you need it. It is also Python and is slightly faster.
Python
x
104
104
1
#!/usr/bin/python3
2
import os
3
import time
4
import datetime
5
import getpass
6
import cv2
7
from PIL import Image
8
9
"""
10
Requires:
11
OpenCV2
12
PIL
13
14
Install via pip or apt.
15
"""
16
17
18
def get_framecv(in_vid, out_vid, w=192, h=108):
19
"""Generate thumbnail."""
20
# Opencv create thumb.
21
try:
22
cap = cv2.VideoCapture(in_vid)
23
video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) - 1
24
print('length',video_length)
25
while cap.isOpened():
26
try:
27
ret, frame = cap.read()
28
cv2.imwrite(out_vid, frame)
29
break
30
except:
31
pass
32
cap.release()
33
# PIL: resize
34
img = Image.open(out_vid) # image extension *.png,*.jpg
35
img = img.resize((w, h), Image.ANTIALIAS)
36
img.save(out_vid)
37
except Exception as e:
38
return e
39
40
def is_video_file(filename, extensions):
41
"""Filter videos"""
42
return any(filename.lower().endswith(e) for e in extensions)
43
44
def scan_vids(base_dir, extensions):
45
"""Walk path and get all videos in path."""
46
for dirpath, dirs, files in os.walk(base_dir):
47
for filename in files:
48
if is_video_file(filename, extensions):
49
full_path = os.path.join(dirpath,filename)
50
date_object = datetime.datetime.fromtimestamp(os.path.getctime(full_path))
51
yield os.path.join(dirpath,filename),date_object.date()
52
53
def gen_thumbs(path,extensions,output=None):
54
"""Worker function"""
55
img_date = datetime.datetime.now().date()
56
associations = []
57
58
if output:
59
img_store = output
60
else:
61
#img_store = r'/home/%s/video_thumbs' % getpass.getuser()
62
img_store = os.path.join(path, 'thumbnails')
63
64
if not os.path.exists(img_store):
65
os.mkdir(img_store)
66
67
for vid_info in scan_vids(path, extensions):
68
vid_path,vid_date = vid_info
69
vid_name = os.path.split(vid_path)[-1].split('.')[0]
70
img_path = os.path.join(img_store,'thumb_%s.png' % vid_name)
71
err = get_framecv(vid_path,img_path)
72
73
if err:
74
continue # Handle errors.
75
76
associations.append((img_path, img_date, vid_path, vid_date))
77
78
return associations
79
80
81
if __name__ == '__main__':
82
83
filter_extensions = ['.mov', '.mp4']
84
local_path = None # Set path to run from program.
85
86
if local_path:
87
user_path = local_path
88
else:
89
# Run from terminal.
90
user_path = None
91
92
user_path = input('Path to videos: ')
93
94
if not user_path:
95
print('Path required.')
96
os._exit(0)
97
if not os.path.exists(user_path):
98
print('Supplied path %s is invalid.' % user_path)
99
os._exit(0)
100
101
# Run...
102
results = gen_thumbs(user_path, filter_extensions, output=None)
103
# Print results.
104
for i in results: print(i)