forked from mohitbooraget/New_Txt_Random
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_video_info.py
44 lines (44 loc) · 113 KB
/
get_video_info.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
""" Get video attributes and thumbnail """ #Bot Created by @NtrRazYt
#Bot Created by @NtrRazYt #Bot Created by @NtrRazYt
import tempfile #Bot Created by @NtrRazYt
from subprocess import getstatusoutput #Bot Created by @NtrRazYt
#Bot Created by @NtrRazYt
#Bot Created by @NtrRazYt
def get_video_attributes(file: str): #Bot Created by @NtrRazYt
"""Returns video duration, width, height""" #Bot Created by @NtrRazYt
#Bot Created by @NtrRazYt
class FFprobeAttributesError(Exception): #Bot Created by @NtrRazYt
"""Exception if ffmpeg fails to generate attributes""" #Bot Created by @NtrRazYt
#Bot Created by @NtrRazYt
cmd = ( #Bot Created by @NtrRazYt
"ffprobe -v error -show_entries format=duration " #Bot Created by @NtrRazYt
+ "-of default=noprint_wrappers=1:nokey=1 " #Bot Created by @NtrRazYt
+ "-select_streams v:0 -show_entries stream=width,height " #Bot Created by @NtrRazYt
+ f" -of default=nw=1:nk=1 '{file}'" #Bot Created by @NtrRazYt
) #Bot Created by @NtrRazYt
res, out = getstatusoutput(cmd) #Bot Created by @NtrRazYt
if res != 0: #Bot Created by @NtrRazYt
raise FFprobeAttributesError(out) #Bot Created by @NtrRazYt
width, height, dur = out.split("\n") #Bot Created by @NtrRazYt
return (int(float(dur)), int(width), int(height)) #Bot Created by @NtrRazYt
#Bot Created by @NtrRazYt
#Bot Created by @NtrRazYt
def get_video_thumb(file: str): #Bot Created by @NtrRazYt
"""Returns path to video thumbnail""" #Bot Created by @NtrRazYt
#Bot Created by @NtrRazYt
class FFprobeThumbnailError(Exception): #Bot Created by @NtrRazYt
"""Exception if ffmpeg fails to generate thumbnail""" #Bot Created by @NtrRazYt
#Bot Created by @NtrRazYt
thumb_file = tempfile.NamedTemporaryFile(suffix=".jpg").name #Bot Created by @NtrRazYt
duration, width, height = get_video_attributes(file) #Bot Created by @NtrRazYt
dur = str(int(duration / 2)) #Bot Created by @NtrRazYt
size = f"{width}x{height}" #Bot Created by @NtrRazYt
cmd = ( #Bot Created by @NtrRazYt
f"ffmpeg -v error -ss {dur} -i '{file}' -vframes 1 " #Bot Created by @NtrRazYt
+ f"-s {size} {thumb_file}" #Bot Created by @NtrRazYt
) #Bot Created by @NtrRazYt
res, out = getstatusoutput(cmd) #Bot Created by @NtrRazYt
if res != 0: #Bot Created by @NtrRazYt
raise FFprobeThumbnailError(out) #Bot Created by @NtrRazYt
return thumb_file #Bot Created by @NtrRazYt
#Bot Created by @NtrRazYt