-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultimedia.py
94 lines (72 loc) · 2.85 KB
/
Multimedia.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
import sys
import ffmpeg as fm
from os import remove
from pathlib import Path
from datetime import date
from threading import Thread
from Lib import existsThread
from time import strftime, localtime
class Download_File():
tempFiles = 'Temp'
root = Path('/').absolute().joinpath(tempFiles)
fileType = {'audio': ['.mp3'], 'video': ['.mp4', '.avi', '.mkv']}
def __init__(self):
self._openDirectory()
def _openDirectory(self) -> bool:
try:
self.root.mkdir(parents=True, exist_ok=True)
print(f'Directorio "{self.root}" creado exitosamente.')
return True
except Exception as e:
print(f'No se pudo crear el directorio "{self.root}": {e}')
return False
def outputFile(self, title: str, outputPath: str = None, **kwargs) -> bool:
sys.stdin.flush(); sys.stdout.flush()
title = title.replace('/', '_')[:]
if outputPath == None: outputPath = str(Path(__file__).parent)
fileName = str(date.today()).replace('-', '_') + strftime("_at_%I-%M-%S-%p", localtime())
abr = kwargs['abr'] * 1000
audio = kwargs['audio']
format = kwargs['format']
audioPath = audio.download(output_path=str(self.root), filename=fileName, filename_prefix='a_')
audio_stream = fm.input(filename=r'{}'.format(audioPath))
finalFile = outputPath + '/' + title + format
if format in self.fileType['video']:
video = kwargs['video']
videoPath = video.download(output_path=str(self.root), filename=fileName, filename_prefix='v_')
video_stream = fm.input(filename=r'{}'.format(videoPath))
(
fm
.output(audio_stream,
video_stream,
finalFile,
threads=9,
acodec='aac',
audio_bitrate=abr,
vcodec='h264',
bufsize='6.5M',
metadata=f'title={title}')
.run(overwrite_output=False)
)
remove(videoPath); remove(audioPath)
return True
(
fm
.output(audio_stream,
finalFile,
threads=3,
acodec='libmp3lame',
audio_bitrate=abr,
bufsize=(abr*1.5),
metadata=f'title={title}')
.run(overwrite_output=False)
)
remove(audioPath)
return True
def saveFile(self, title, outputPath, **kwargs):
exists = existsThread('Thread-2')
if not exists:
self.threadSave = Thread(target=self.outputFile, name='Thread-2', args=(title, outputPath), kwargs=kwargs, daemon=True)
self.threadSave.start()
else:
return 0