Last active 1625145369

A script to get total duration of files provided in arguments. Python3 and ffprobe

Alyssa Smith revised this gist 1625181369. Go to revision

1 file changed, 1 insertion, 1 deletion

dur

@@ -5,7 +5,7 @@ from math import floor
5 5
6 6 def hms(inp):
7 7 s,ms = divmod(inp,1)
8 - m,s = divmod(inp,60)
8 + m,s = divmod(s,60)
9 9 h,m = divmod(m,60)
10 10 return f"{h:02.0f}:{m:02.0f}:{s:02.0f}.{floor(ms*100):02.0f}"
11 11

Alyssa Smith revised this gist 1625181335. Go to revision

1 file changed, 3 insertions, 2 deletions

dur

@@ -1,12 +1,13 @@
1 1 #!/usr/bin/env python3
2 2 from sys import stderr, argv, exit
3 3 import subprocess
4 + from math import floor
4 5
5 6 def hms(inp):
7 + s,ms = divmod(inp,1)
6 8 m,s = divmod(inp,60)
7 9 h,m = divmod(m,60)
8 - return f"{str(int(h)).zfill(2)}:{str(int(m)).zfill(2)}:{str(round(s,2)).zfill(2)}"
9 - # return ":".join(map(str,(int(h),int(m),round(s,2))))
10 + return f"{h:02.0f}:{m:02.0f}:{s:02.0f}.{floor(ms*100):02.0f}"
10 11
11 12 def get_video_duration(fileloc) :
12 13 command = ['ffprobe',

Alyssa Smith revised this gist 1625180412. Go to revision

1 file changed, 36 insertions

dur(file created)

@@ -0,0 +1,36 @@
1 + #!/usr/bin/env python3
2 + from sys import stderr, argv, exit
3 + import subprocess
4 +
5 + def hms(inp):
6 + m,s = divmod(inp,60)
7 + h,m = divmod(m,60)
8 + return f"{str(int(h)).zfill(2)}:{str(int(m)).zfill(2)}:{str(round(s,2)).zfill(2)}"
9 + # return ":".join(map(str,(int(h),int(m),round(s,2))))
10 +
11 + def get_video_duration(fileloc) :
12 + command = ['ffprobe',
13 + '-v', 'fatal',
14 + '-show_entries', 'stream=duration',
15 + '-of', 'default=noprint_wrappers=1:nokey=1',
16 + fileloc, '-sexagesimal']
17 + ffmpeg = subprocess.Popen(command, stderr=subprocess.PIPE ,stdout = subprocess.PIPE )
18 + out, err = ffmpeg.communicate()
19 + if(err) : print(err)
20 + out = out.decode('utf-8').strip()
21 + return map(float,out.split(":"))
22 +
23 + def main():
24 + tot = 0.0
25 + for fn in argv[1:]:
26 + h,m,s = get_video_duration(fn)
27 + tot += h*60*60 + m*60 + s
28 + print(f"{hms(tot)}", end="\r", file=stderr)
29 + print(hms(tot))
30 + return 0
31 +
32 + if __name__ == "__main__":
33 + try:
34 + exit(main())
35 + except KeyboardInterrupt:
36 + exit(127)
Newer Older