Spaces:
Running
Running
File size: 15,882 Bytes
23d4cfa 0b484f3 23d4cfa af64706 23d4cfa 8bec4e8 1c45af0 3e04bc5 8fc9419 11c4b73 a02cdd0 23d4cfa f834df3 8451859 f834df3 3e04bc5 8451859 3e04bc5 8451859 3e04bc5 f834df3 3e04bc5 f834df3 3e04bc5 f834df3 3e04bc5 f834df3 23d4cfa a02cdd0 e9724c9 2f6410a e9724c9 fc4e5a1 e9724c9 fc4e5a1 e9724c9 fc4e5a1 1c45af0 e9724c9 fc4e5a1 8451859 fc4e5a1 a02cdd0 fc4e5a1 a02cdd0 8bec4e8 23d4cfa 8bec4e8 23d4cfa 105d730 23d4cfa 3f4d8ed 23d4cfa 8451859 23d4cfa 3f4d8ed 23d4cfa e9c5837 462a507 e9c5837 2ae1d12 e9c5837 c980ffd a4ca082 c980ffd e9c5837 c980ffd e9c5837 c980ffd 23d4cfa c980ffd 4927d22 23d4cfa 8451859 23d4cfa a05e96e 23d4cfa 97d83d9 23d4cfa da7713e 39186e9 eda06b1 23d4cfa 58f3a48 23d4cfa 6469880 23d4cfa 5917224 94d9021 9db11d1 f834df3 8bec4e8 a02cdd0 8bec4e8 f1ad5bd 1e003de 504809d f1ad5bd 1e003de f1ad5bd 8bec4e8 3e04bc5 8bec4e8 ea41cec f834df3 23d4cfa f834df3 d74463a f834df3 23d4cfa 58f3a48 11dd586 2f8afe5 8451859 d5458fa ea41cec 2f6410a 1584125 23d4cfa 1584125 23d4cfa 9db11d1 0aa9031 f834df3 |
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
from pytubefix import YouTube
from pytubefix.cli import on_progress
import time
import math
import gradio as gr
import ffmpeg
from faster_whisper import WhisperModel
import requests
import json
import arabic_reshaper # pip install arabic-reshaper
from bidi.algorithm import get_display # pip install python-bidi
from moviepy import VideoFileClip, TextClip, CompositeVideoClip, AudioFileClip, ImageClip
import pysrt
import instaloader
import time
import concurrent.futures
import re
from io import BytesIO
from PIL import Image
api_key = "268976:66f4f58a2a905"
def fetch_data(url):
try:
response = requests.get(url)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None
def download_file(url):
try:
response = requests.get(url.split("#")[0], stream=True)
response.raise_for_status()
print(url.split("#")[1])
with open(url.split("#")[1], 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
file.write(chunk)
file.close()
print(f"Downloaded successfully: {url.split('#')[1]}")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
def download_chunk(url, start, end, filename, index):
headers = {'Range': f'bytes={start}-{end}'}
response = requests.get(url, headers=headers, stream=True)
response.raise_for_status()
chunk_filename = f'{filename}.part{index}'
with open(chunk_filename, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
file.write(chunk)
file.close()
return chunk_filename
def merge_files(filename, num_parts):
with open(filename, 'wb') as output_file:
for i in range(num_parts):
part_filename = f'{filename}.part{i}'
with open(part_filename, 'rb') as part_file:
output_file.write(part_file.read())
# Optionally, delete the part file after merging
# os.remove(part_filename)
part_file.close()
def download_file_in_parallel(link, size, num_threads=4):
url = link.split("#")[0]
filename = link.split("#")[1]
print(url+" filename: "+filename)
response = requests.head(url)
#file_size = int(response.headers['Content-Length'])
chunk_size = size // num_threads
ranges = [(i * chunk_size, (i + 1) * chunk_size - 1) for i in range(num_threads)]
ranges[-1] = (ranges[-1][0], size - 1) # Adjust the last range to the end of the file
with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
futures = [
executor.submit(download_chunk, url, start, end, filename, i)
for i, (start, end) in enumerate(ranges)
]
for future in concurrent.futures.as_completed(futures):
future.result() # Ensure all threads complete
merge_files(filename, num_threads)
print(f'Downloaded successfully: {filename}')
def one_youtube(link, api_key):
# Fetch video ID
video_id_url = f"https://one-api.ir/youtube/?token={api_key}&action=getvideoid&link={link}"
video_data = fetch_data(video_id_url)
if not video_data:
return None, None
video_id = video_data["result"]
# Fetch video data
filter_option = "" # Replace with your filter option
video_data_url = f"https://youtube.one-api.ir/?token={api_key}&action=fullvideo&id={video_id}&filter={filter_option}"
video_data_2 = fetch_data(video_data_url)
if not video_data_2:
return None, None
formats_list = video_data_2["result"]["formats"]
file_name = video_data_2["result"]["title"]
video_name = f'{file_name}.mp4'
audio_name = f'{file_name}.mp3'
for f in formats_list:
if f["format_note"] == "360p":
download_id = f["id"]
video_size = f["filesize"]
for f in formats_list:
if f["format_note"] == "medium":
audio_id = f["id"]
audio_size = f["filesize"]
if not download_id or not audio_id:
return None, None
# Fetch video and audio links
video_link_url = f"https://youtube.one-api.ir/?token={api_key}&action=download&id={download_id}"
audio_link_url = f"https://youtube.one-api.ir/?token={api_key}&action=download&id={audio_id}"
video_link_data = fetch_data(video_link_url)
audio_link_data = fetch_data(audio_link_url)
if not video_link_data or not audio_link_data:
return None, None
video_link = video_link_data["result"]["link"]
audio_link = audio_link_data["result"]["link"]
vid_str=video_link+"#"+video_name
audio_str=audio_link+"#"+audio_name
# Download video and audio files
print(video_size , audio_size)
download_file_in_parallel(vid_str, video_size)
download_file_in_parallel(audio_str, audio_size)
return video_name, audio_name
# Define your functions here
def yt_download(url):
yt = YouTube(url)
print(yt.title)
video_path = f"{yt.title}.mp4"
ys = yt.streams.get_highest_resolution()
print(ys)
ys.download()
return video_path, yt.title
def download_image(url, save_path='downloaded_image.jpg'):
response = requests.get(url)
image = Image.open(BytesIO(response.content))
image.save(save_path)
return save_path
def insta_oneapi(url, api_key):
shortcode = url.split("/")[-2]
print(shortcode)
url_one="https://api.one-api.ir/instagram/v1/post/?shortcode="+shortcode
request_body = [{"shortcode": shortcode},]
headers = {"one-api-token": api_key, "Content-Type": "application/json"}
response = requests.get(url_one, headers=headers)
print(response)
if response.status_code == 200:
result = response.json()
try:
time.sleep(10)
response = requests.get(result["result"]['media'][0]["url"], stream=True)
response.raise_for_status()
with open("video.mp4", 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
file.write(chunk)
file.close()
print(f"Downloaded successfully")
image_url = result["result"]['media'][0]["cover"]
image_file_path = download_image(image_url)
return "video.mp4", image_file_path
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
else:
print(f"Error: {response.status_code}, {response.text}")
return None
def insta_download(permalink):
# Create an instance of Instaloader
L = instaloader.Instaloader()
try:
# Extract the shortcode from the permalink
if "instagram.com/reel/" in permalink:
shortcode = permalink.split("instagram.com/reel/")[-1].split("/")[0]
elif "instagram.com/p/" in permalink:
shortcode = permalink.split("instagram.com/p/")[-1].split("/")[0]
else:
raise ValueError("Invalid permalink format")
# Load the post using the shortcode
post = instaloader.Post.from_shortcode(L.context, shortcode)
# Check if the post is a video
if not post.is_video:
raise ValueError("The provided permalink is not a video.")
# Get the video URL
video_url = post.video_url
# Extract the filename from the URL
filename = video_url.split("/")[-1]
# Remove query parameters
filename = filename.split("?")[0]
# Download the video using requests
response = requests.get(video_url, stream=True)
response.raise_for_status() # Raise an error for bad responses
# Save the content to a file
with open(filename, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
print(f"Downloaded video {filename} successfully.")
return filename
except Exception as e:
print(f"Failed to download video from {permalink}: {e}")
def extract_audio(input_video_name):
# Define the input video file and output audio file
mp3_file = "audio.mp3"
# Load the video clip
video_clip = VideoFileClip(input_video_name)
# Extract the audio from the video clip
audio_clip = video_clip.audio
# Write the audio to a separate file
audio_clip.write_audiofile(mp3_file)
# Close the video and audio clips
audio_clip.close()
video_clip.close()
print("Audio extraction successful!")
return mp3_file
def transcribe(audio):
model = WhisperModel("tiny")
segments, info = model.transcribe(audio)
segments = list(segments)
for segment in segments:
print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
return segments
def format_time(seconds):
hours = math.floor(seconds / 3600)
seconds %= 3600
minutes = math.floor(seconds / 60)
seconds %= 60
milliseconds = round((seconds - math.floor(seconds)) * 1000)
seconds = math.floor(seconds)
formatted_time = f"{hours:02d}:{minutes:02d}:{seconds:01d},{milliseconds:03d}"
return formatted_time
def generate_subtitle_file(language, segments, input_video_name):
subtitle_file = f"sub-{input_video_name}.{language}.srt"
text = ""
for index, segment in enumerate(segments):
segment_start = format_time(segment.start)
segment_end = format_time(segment.end)
text += f"{str(index+1)} \n"
text += f"{segment_start} --> {segment_end} \n"
text += f"{segment.text} \n"
text += "\n"
f = open(subtitle_file, "w", encoding='utf8')
f.write(text)
f.close()
return subtitle_file
def read_srt_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
srt_content = file.read()
file.close()
return srt_content
except FileNotFoundError:
print(f"The file {file_path} was not found.")
except Exception as e:
print(f"An error occurred: {e}")
def clean_text(text):
# Remove 'srt ' from the start of each line
# Remove ''' from the start and end
text = re.sub(r"^```|```$", '', text)
text = re.sub(r'^srt', '', text, flags=re.MULTILINE)
return text
def enhance_text(api_key, text, google):
url = "https://api.one-api.ir/chatbot/v1/gpt4o/"
# Prepare the request body
request_body = [{
"role": "user",
"content": f"{text} Translate the above text into Persian, converting the English terms used in it into common Persian terms. in respose dont add any thing exept for the srt formated translation."
},]
# Add the API key to the request
headers = {
"one-api-token": api_key,
"Content-Type": "application/json"
}
# Make the POST request
response = requests.post(url, headers=headers, json=request_body)
# Check the response status
if response.status_code == 200:
result = response.json()
clean_text(result["result"][0])
last = clean_text(result["result"][0])
print("result: ")
print(last)
return last
else:
print(f"Error: {response.status_code}, {response.text}")
return None
def translate_text(api_key, source_lang, target_lang, text):
url = "https://api.one-api.ir/translate/v1/google/"
request_body = {"source": source_lang, "target": target_lang, "text": text}
headers = {"one-api-token": api_key, "Content-Type": "application/json"}
response = requests.post(url, headers=headers, json=request_body)
if response.status_code == 200:
result = response.json()
enhanced_text = enhance_text(api_key, text, result['result'])
return enhanced_text
else:
print(f"Error: {response.status_code}, {response.text}")
return None
def write_google(google_translate):
google = "google_translate.srt"
with open(google, 'w', encoding="utf-8") as f:
f.write(google_translate)
f.close()
def time_to_seconds(time_obj):
return time_obj.hours * 3600 + time_obj.minutes * 60 + time_obj.seconds + time_obj.milliseconds / 1000
def create_subtitle_clips(subtitles, videosize, fontsize, font, color, debug):
subtitle_clips = []
for subtitle in subtitles:
start_time = time_to_seconds(subtitle.start) # Add 2 seconds offset
end_time = time_to_seconds(subtitle.end)
duration = end_time - start_time
video_width, video_height = videosize
max_width = video_width * 0.8
max_height = video_height * 0.2
#reshaped_text = arabic_reshaper.reshape(subtitle.text)
#bidi_text = get_display(reshaped_text)
text_clip = TextClip(font, subtitle.text, font_size=fontsize, size=(int(video_width * 0.8), int(video_height * 0.2)) ,text_align="center" ,color=color, method='caption').with_start(start_time).with_duration(duration)
subtitle_x_position = 'center'
subtitle_y_position = video_height * 0.68
text_position = (subtitle_x_position, subtitle_y_position)
subtitle_clips.append(text_clip.with_position(text_position))
return subtitle_clips
def process_video(url, type):
if type=="insta":
input_video, image_path=insta_oneapi(url, api_key)
input_video_name = input_video.replace(".mp4", "")
video = VideoFileClip(input_video)
image_clip = ImageClip(image_path).with_duration(1)
# Set the position and size of the image (optional)
image_clip = image_clip.with_position(("center", "center")).resized(height=video.size[1])
first_video = CompositeVideoClip([video.with_start(1), image_clip])
input_video = input_video_name+"_cover.mp4"
input_video_name = input_video.replace(".mp4", "")
first_video.write_videofile(input_video, codec="libx264", audio_codec="aac", logger=None)
input_audio = extract_audio(input_video)
elif type=="youtube":
input_video, input_audio = one_youtube(url, api_key)
input_video_name = input_video.replace(".mp4", "")
# Get the current local time
t = time.localtime()
# Format the time as a string
current_time = time.strftime("%H:%M:%S", t)
print("Current Time =", current_time)
segments = transcribe(audio=input_audio)
language = "fa"
subtitle_file = generate_subtitle_file(language=language, segments=segments, input_video_name=input_video_name)
source_language = "en"
target_language = "fa"
srt_string = read_srt_file(subtitle_file)
google_translate = translate_text(api_key, source_language, target_language, srt_string)
write_google(google_translate)
video = VideoFileClip(input_video)
audio = AudioFileClip(input_audio)
video = video.with_audio(audio)
print(video)
subtitles = pysrt.open("google_translate.srt", encoding="utf-8")
output_video_file = input_video_name + '_subtitled' + ".mp4"
subtitle_clips = create_subtitle_clips(subtitles, video.size, 32, 'arial.ttf', 'white', False)
final_video = CompositeVideoClip([video] + subtitle_clips)
final_video.write_videofile(output_video_file, codec="libx264", audio_codec="aac", logger=None)
video.close()
audio.close()
print('final')
# Get the current local time
t = time.localtime()
# Format the time as a string
current_time = time.strftime("%H:%M:%S", t)
print("Current Time =", current_time)
# Generate the URL for the file
return output_video_file
def download_file(file_path):
return gr.File.update(file_path)
iface = gr.Interface(fn=process_video, inputs=[gr.Text(),gr.Dropdown(["insta","youtube"])], outputs="file")
iface.launch(debug=True) |