-
Notifications
You must be signed in to change notification settings - Fork 196
/
split.py
91 lines (82 loc) · 3.21 KB
/
split.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
from os import remove as osremove, path as ospath, walk
from json import loads as jsnloads
from subprocess import run as srun, check_output
from math import ceil
import os
import json
# conifg
with open('config.json', 'r') as f: data = json.load(f)
# premium account
temp_channel = os.environ.get("TEMP_CHAT") or data.get("TEMP_CHAT", "")
try: temp_channel = int(temp_channel)
except: pass
ss = os.environ.get("STRING") or data.get("STRING", "")
if ss != "" and temp_channel != "": isPremmium = True
else: isPremmium = False
TG_SPLIT_SIZE = 2097151000
checksize = 2097152000
if isPremmium:
TG_SPLIT_SIZE *= 2
checksize *= 2
VIDEO_SUFFIXES = ("M4V", "MP4", "MOV", "FLV", "WMV", "3GP", "MPG", "WEBM", "MKV", "AVI")
def get_media_info(path):
try:
result = check_output(["./ffmpeg/ffprobe", "-hide_banner", "-loglevel", "error", "-print_format",
"json", "-show_format", path]).decode('utf-8')
fields = jsnloads(result)['format']
except Exception as e:
print(f"get_media_info: {e}")
return 0, None, None
try:
duration = round(float(fields['duration']))
except:
duration = 0
try:
artist = str(fields['tags']['artist'])
except:
artist = None
try:
title = str(fields['tags']['title'])
except:
title = None
return duration, artist, title
def get_path_size(path: str):
if ospath.isfile(path):
return ospath.getsize(path)
total_size = 0
for root, dirs, files in walk(path):
for f in files:
abs_path = ospath.join(root, f)
total_size += ospath.getsize(abs_path)
return total_size
def split_file(path, size, file_, dirpath, split_size, start_time=0, i=1):
parts = ceil(size/TG_SPLIT_SIZE)
flist = []
if file_.upper().endswith(VIDEO_SUFFIXES):
base_name, extension = ospath.splitext(file_)
split_size = split_size - 2500000
while i <= parts :
parted_name = "{}.part{}{}".format(str(base_name), str(i).zfill(3), str(extension))
out_path = ospath.join(dirpath, parted_name)
srun(["./ffmpeg/ffmpeg", "-hide_banner", "-loglevel", "error", "-i",
path, "-ss", str(start_time), "-fs", str(split_size),
"-async", "1", "-strict", "-2", "-c", "copy", out_path])
out_size = get_path_size(out_path)
if out_size > checksize:
dif = out_size - checksize
split_size = split_size - dif + 2500000
osremove(out_path)
return split_file(path, size, file_, dirpath, split_size, start_time, i)
lpd = get_media_info(out_path)[0]
if lpd <= 4 or out_size < 1000000:
osremove(out_path)
break
start_time += lpd - 3
i = i + 1
flist.append(out_path)
else:
out_path = ospath.join(dirpath, file_ + ".")
srun(["split", "--numeric-suffixes=1", "--suffix-length=3", f"--bytes={split_size}", path, out_path])
flist.append(out_path)
print(flist)
return flist