-
Notifications
You must be signed in to change notification settings - Fork 0
/
vidlength.py
102 lines (82 loc) · 2.03 KB
/
vidlength.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
93
94
95
96
97
98
99
100
101
102
import os
import json
import subprocess
from pathlib import Path
from datetime import timedelta
from ranger.api.commands import Command
video_extensions = (
".mp4",
".mkv",
".avi",
".webm",
".mpg",
".mp2",
".mpeg",
".mpe",
".mpv",
".ogg",
".mp4",
".m4p",
".m4v",
".wmv",
".mov",
".qt",
".flv",
".swf",
)
def get_length(path: Path) -> float:
cmd = [
"ffprobe",
"-v",
"quiet",
"-print_format",
"json",
"-show_entries",
"format=duration",
path,
]
output = subprocess.check_output(cmd)
json_output = json.loads(output)
length = json_output["format"]["duration"]
return float(length)
def format_seconds(seconds: float) -> str:
delta = timedelta(seconds=seconds)
return str(delta)
def is_video(path: str):
return os.path.splitext(path)[1].lower() in video_extensions
def get_files_recursive(path: Path, filter) -> list:
files = [
os.path.join(dp, f)
for dp, _, filenames in os.walk(path)
for f in filenames
if filter(f)
]
return files
def get_videos(selected: list) -> list:
videos = []
for video in selected:
path = Path(video)
if path.is_dir():
files = get_files_recursive(path, is_video)
videos.extend(files)
elif is_video(video):
videos.append(video)
return videos
def get_total_length(videos: list) -> float:
total_length = 0
for video in videos:
length = get_length(video)
total_length += length
return total_length
class vidlength(Command):
""":vidlength
Get the length of selected videos recursively
"""
def execute(self):
selected = [f.path for f in self.fm.thistab.get_selection()] or [
self.fm.thisfile.path
]
videos = get_videos(selected)
total_length = get_total_length(videos)
formatted_length = format_seconds(total_length)
self.fm.notify(formatted_length)