-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviduration.py
executable file
·63 lines (57 loc) · 1.56 KB
/
viduration.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
#!/bin/env python
import os,sys,glob,getopt
from colorama import init, Fore, Back
from pprint import pprint
import ffmpeg
import subprocess
init()
def errprint(str):
print(str, file=sys.stderr)
def prun(cmd,track):
print(f"({track})"+Fore.YELLOW+cmd+Fore.RESET)
scmd = cmd.split()
process = subprocess.Popen(scmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if process.returncode != 0:
print(stderr)
raise RuntimeError(stderr)
def showhelp():
print("help")
rs = '''
-h, --help show help
-v, --videofile filename
-k, --key only 'all' supported, default is 'duration'
'''
print(rs)
exit()
filename = False
keyname = "duration"
argv = sys.argv[1:]
try:
opts, args = getopt.getopt(argv, "hv:k:", [
"help",
"videofile=",
"key=",
])
except Exception as e:
print(str(e))
for opt, arg in opts:
if opt in ("-h", "--help"):
showhelp();
if opt in ("-v", "--videofile"):
filename = arg
if opt in ("-k", "--key"):
keyname = arg
if filename == False:
print(f"missing filename pof switch (-v)")
exit()
# errprint(f"viduration: filename: {filename}")
probe = ffmpeg.probe(filename)
video_streams = [stream for stream in probe["streams"] if stream["codec_type"] == "video"]
if keyname == "all":
pprint(video_streams[0])
exit()
else:
dur_sec = video_streams[0]['duration']
dur_ts = video_streams[0]['duration_ts']
print(int(round(float(dur_sec))), dur_sec, filename,dur_ts)