-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy paththumbnail_video.py
92 lines (75 loc) · 2.03 KB
/
thumbnail_video.py
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
# GOAL:
# getting track for logging
import logging
LOGGER = logging.getLogger(__name__)
# GOAL:
# create video thumbnail maker handler class
from os import path as os_path, rename as os_rename, remove as os_remove
import asyncio
from ffprobe import stream_creator
async def thumb_creator(filepath):
if not os_path.exists(filepath):
LOGGER.error('File not found : ' + filepath)
return False
probe = await stream_creator(filepath)
video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
try:
duration = float(video_stream["duration"]) // 2
except:
duration = 0
out_file = filepath + ".jpg"
cmd = [
"ffmpeg",
"-hide_banner",
'-ss',
str(duration),
"-i",
filepath,
'-vframes',
'1',
'-vf',
'scale=320:-1',
'-y',
out_file
]
LOGGER.debug(cmd)
process = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await process.communicate()
LOGGER.debug('Thumbnail : ' + out_file)
return out_file
async def set(filepath):
if not os_path.exists(filepath):
LOGGER.error('File not found : ' + filepath)
return False
prepare_path = filepath + '.prep'
os_rename(filepath, prepare_path)
cmd = [
"ffmpeg",
"-hide_banner",
"-i",
prepare_path,
'-vf',
'scale=320:-1',
'-y',
filepath
]
LOGGER.debug(cmd)
process = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await process.communicate()
os_remove(prepare_path)
LOGGER.debug('Set thumbnail : ' + filepath)
return True
async def reset(filepath):
if not os_path.exists(filepath):
LOGGER.error('File not found : ' + filepath)
return True
os_remove(filepath)
return True