-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper.py
57 lines (41 loc) · 1.42 KB
/
helper.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
import json
import os
import subprocess
def read_file(path):
assert isinstance(path, str), "Path must be a string"
try:
with open(path, 'r', encoding='utf-8') as file:
content = file.read()
return content
except FileNotFoundError:
return "Error: File not found."
except Exception as e:
return f"Error: {str(e)}"
def get_video_duration_from_file(video_path):
try:
result = subprocess.run(
['ffprobe', '-v', 'error', '-show_format', '-show_streams', '-of', 'json', video_path],
stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
video_info = json.loads(result.stdout)
duration = float(video_info['format']['duration'])
return duration
except Exception as e:
print(f"Can't fetch Video-Duration: {str(e)}")
return 0
def format_duration(seconds):
minutes = seconds // 60
seconds = seconds % 60
return f"{minutes}:{str(seconds).zfill(2)}"
def get_file_size(file_path):
return os.path.getsize(file_path)
def get_range(file_path, byte_range):
with open(file_path, 'rb') as f:
f.seek(byte_range[0])
return f.read(byte_range[1] - byte_range[0] + 1)
def get_api_key():
try:
with open("token.txt", "r") as f:
return f.read().strip()
except FileNotFoundError:
raise FileNotFoundError("Missing token.txt. Please go to README.md")