Repository navigation
Expand file tree
/
Copy pathpreview_utils.py
More file actions
67 lines (53 loc) · 2.2 KB
/
Copy pathpreview_utils.py
File metadata and controls
67 lines (53 loc) · 2.2 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
import io
import os
import eyed3
from PIL import Image
from moviepy.editor import VideoFileClip
from hash_utils import generate_unique_string
from thread_utils import TaskWrapper
def generate_thumbnail(input_file, output_file, logger: TaskWrapper = None):
"""
Make a thumbnail
:param input_file: The input file
:param output_file: Where to trite the thumbnail
:param logger: the logger, required
:return: Nothing
"""
_, ext = os.path.splitext(input_file)
if ext.lower() in ['.mp4', '.m4v', '.mpg', '.avi', '.mkv', '.webm']: # Video file
clip = VideoFileClip(input_file)
duration = clip.duration
thumbnail_time = duration * 0.1 # 10% playback position
frame = clip.get_frame(thumbnail_time)
resized_frame = Image.fromarray(frame)
resized_frame.thumbnail((256, 256))
resized_frame.save(output_file)
clip.close()
elif ext.lower() == '.mp3': # MP3 file
audiofile = eyed3.load(input_file)
for image in audiofile.tag.images:
try:
cover_art = Image.open(io.BytesIO(image.image_data))
cover_art.thumbnail((256, 256))
cover_art.save(output_file)
return
except Exception as inst:
logger.error(str(inst))
class CreatePreviewTask(TaskWrapper):
def __init__(self, name, description, source_folder, preview_folder):
super().__init__(name, description)
self.source_folder = source_folder
self.preview_folder = preview_folder
def run(self, db_session):
for root, dirs, files in os.walk(self.source_folder):
for file_name in files:
# Construct the full path to the file
file_path = os.path.join(root, file_name)
if self.is_cancelled:
self.critical('Cancelled')
return
if os.path.isfile(file_path):
hash_name = generate_unique_string(file_path)
hash_file = os.path.join(self.preview_folder, hash_name + '.png')
if not os.path.exists(hash_file):
generate_thumbnail(file_path, hash_file, self)