-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiktok.py
146 lines (126 loc) · 5.58 KB
/
tiktok.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
import logging
import yt_dlp
import uuid
import time
import subprocess
from telegram import InputFile
from telegram.ext import ContextTypes
from config import DOWNLOAD_DIR
from platforms.proxy_manager import retry_with_sequential_proxy # Importăm modulul proxy_manager
logger = logging.getLogger(__name__)
async def send_video_to_user(context: ContextTypes.DEFAULT_TYPE, chat_id: int, video_file: str) -> None:
try:
if video_file and os.path.exists(video_file):
logger.info(f'Trimiterea videoclipului către utilizator... File: {video_file}')
ffprobe_command = [
'ffprobe',
'-v', 'error',
'-select_streams', 'v:0',
'-show_entries', 'stream=width,height',
'-of', 'csv=s=x:p=0',
video_file
]
result = subprocess.run(ffprobe_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
resolution = result.stdout.strip()
if resolution:
width, height = map(int, resolution.split('x'))
logger.info(f'Resolution detected: {width}x{height}')
with open(video_file, 'rb') as vf:
await context.bot.send_video(
chat_id=chat_id,
video=InputFile(vf),
width=width,
height=height
)
logger.info('Videoclip trimis cu succes.')
else:
logger.error('Nu s-a putut detecta rezoluția. Videoclipul nu va fi trimis.')
await context.bot.send_message(chat_id=chat_id, text='Eroare: Nu am putut detecta rezoluția videoclipului. Videoclipul nu va fi trimis.')
else:
logger.error(f'Videoclipul nu a fost găsit: {video_file}')
await context.bot.send_message(chat_id=chat_id, text='Fișierul video nu a fost găsit.')
except TimedOut:
logger.error('Eroare la trimiterea videoclipului: Timed out')
await context.bot.send_message(chat_id=chat_id, text='Eroare la trimiterea videoclipului: Timed out')
except Exception as e:
logger.error(f'Eroare la trimiterea videoclipului: {e}')
await context.bot.send_message(chat_id=chat_id, text=f'Eroare la trimiterea videoclipului: {e}')
finally:
if os.path.exists(video_file):
os.remove(video_file)
def check_and_fix_faststart(input_file):
# Verifică dacă fișierul are flag-ul +faststart
ffmpeg_check_command = [
'ffmpeg',
'-v', 'trace',
'-i', input_file,
'-c', 'copy',
'-f', 'null', '-'
]
try:
result = subprocess.run(ffmpeg_check_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stderr_output = result.stderr
moov_pos = stderr_output.find("type:'moov'")
mdat_pos = stderr_output.find("type:'mdat'")
if moov_pos != -1 and (mdat_pos == -1 or moov_pos < mdat_pos):
logger.info(f"Fișierul {input_file} are deja flag-ul +faststart.")
return input_file
else:
logger.info(f"Fișierul {input_file} nu are flag-ul +faststart. Aplicare flag...")
except subprocess.CalledProcessError as e:
logger.error(f"Eroare la verificarea flag-ului +faststart pentru {input_file}: {e}")
return None
except Exception as e:
logger.error(f"Eroare la citirea ieșirii ffmpeg: {e}")
return None
output_file = input_file.replace(".mp4", "_faststart.mp4")
ffmpeg_command = [
'ffmpeg',
'-y',
'-i', input_file,
'-c', 'copy',
'-preset', 'superfast',
'-movflags', '+faststart',
output_file
]
try:
subprocess.run(ffmpeg_command, stderr=subprocess.PIPE, text=True)
os.remove(input_file)
logger.info(f"Flag-ul +faststart a fost adăugat pentru {output_file}")
return output_file
except subprocess.CalledProcessError as e:
logger.error(f"Eroare la adăugarea flag-ului +faststart pentru {input_file}: {e}")
return None
async def process_tiktok_video(url, context, chat_id):
unique_id = str(uuid.uuid4())
output_file = os.path.join(DOWNLOAD_DIR, f"tiktok_video_{unique_id}.mp4")
try:
download_start_time = time.time()
# Funcția care va fi apelată cu rotația proxy-urilor
def download_video_with_proxy(proxy):
ydl_opts = {
'format': 'bestvideo+bestaudio/best',
'outtmpl': output_file,
'proxy': proxy,
'retries': 2,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
# Reîncercăm descărcarea folosind rotația proxy-urilor
retry_with_sequential_proxy(download_video_with_proxy)
except yt_dlp.utils.DownloadError as e:
logger.error(f"Eroare la descărcare: {e}")
await context.bot.send_message(chat_id=chat_id, text=f"Eroare la descărcarea videoclipului: {e}")
return False
final_file = check_and_fix_faststart(output_file)
if final_file:
await send_video_to_user(context, chat_id, final_file)
return True
else:
logger.error("Eroare în timpul procesării +faststart.")
await context.bot.send_message(chat_id=chat_id, text="Eroare în timpul procesării videoclipului.")
return False
final_end_time = time.time()
total_duration = final_end_time - download_start_time
logger.info(f"Timpul total pentru procesare: {total_duration:.2f} secunde")