Update app.py
Browse files
app.py
CHANGED
|
@@ -9,13 +9,82 @@ import requests
|
|
| 9 |
import json
|
| 10 |
import arabic_reshaper # pip install arabic-reshaper
|
| 11 |
from bidi.algorithm import get_display # pip install python-bidi
|
| 12 |
-
from moviepy import VideoFileClip, TextClip, CompositeVideoClip
|
| 13 |
import pysrt
|
| 14 |
api_key = "268976:66f4f58a2a905"
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Define your functions here
|
| 20 |
def yt_download(url):
|
| 21 |
yt = YouTube(url)
|
|
@@ -108,7 +177,7 @@ def write_google(google_translate):
|
|
| 108 |
def time_to_seconds(time_obj):
|
| 109 |
return time_obj.hours * 3600 + time_obj.minutes * 60 + time_obj.seconds + time_obj.milliseconds / 1000
|
| 110 |
|
| 111 |
-
def create_subtitle_clips(subtitles, videosize, fontsize=
|
| 112 |
subtitle_clips = []
|
| 113 |
for subtitle in subtitles:
|
| 114 |
start_time = time_to_seconds(subtitle.start)
|
|
@@ -117,18 +186,18 @@ def create_subtitle_clips(subtitles, videosize, fontsize=24, font='/content/aria
|
|
| 117 |
video_width, video_height = videosize
|
| 118 |
reshaped_text = arabic_reshaper.reshape(subtitle.text)
|
| 119 |
bidi_text = get_display(reshaped_text)
|
| 120 |
-
text_clip = TextClip(bidi_text, fontsize=fontsize, font=font, color=color, bg_color='black', size=(video_width*3/4, None), method='label', align='
|
| 121 |
subtitle_x_position = 'center'
|
| 122 |
subtitle_y_position = video_height * 4 / 5
|
| 123 |
text_position = (subtitle_x_position, subtitle_y_position)
|
| 124 |
subtitle_clips.append(text_clip.set_position(text_position))
|
| 125 |
return subtitle_clips
|
| 126 |
|
| 127 |
-
def process_video(url
|
| 128 |
-
|
|
|
|
| 129 |
input_video_name = input_video.replace(".mp4", "")
|
| 130 |
-
|
| 131 |
-
segments = transcribe(audio=extracted_audio)
|
| 132 |
language = "fa"
|
| 133 |
subtitle_file = generate_subtitle_file(language=language, segments=segments, input_video_name=input_video_name)
|
| 134 |
source_language = "en"
|
|
@@ -137,16 +206,19 @@ def process_video(url, api_key):
|
|
| 137 |
google_translate = translate_text(api_key, source_language, target_language, srt_string)
|
| 138 |
write_google(google_translate)
|
| 139 |
video = VideoFileClip(input_video)
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
| 141 |
output_video_file = input_video_name + '_subtitled' + ".mp4"
|
| 142 |
subtitle_clips = create_subtitle_clips(subtitles, video.size)
|
| 143 |
final_video = CompositeVideoClip([video] + subtitle_clips)
|
| 144 |
final_video.write_videofile(output_video_file, codec="libx264", audio_codec="aac")
|
| 145 |
-
return output_video_file
|
| 146 |
|
| 147 |
-
def download_file(file_path):
|
| 148 |
-
|
| 149 |
|
| 150 |
-
iface = gr.Interface(fn=process_video, inputs=["text"
|
| 151 |
|
| 152 |
-
iface.launch()
|
|
|
|
| 9 |
import json
|
| 10 |
import arabic_reshaper # pip install arabic-reshaper
|
| 11 |
from bidi.algorithm import get_display # pip install python-bidi
|
| 12 |
+
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip, AudioFileClip
|
| 13 |
import pysrt
|
| 14 |
api_key = "268976:66f4f58a2a905"
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
+
def fetch_data(url):
|
| 20 |
+
try:
|
| 21 |
+
response = requests.get(url)
|
| 22 |
+
response.raise_for_status()
|
| 23 |
+
return response.json()
|
| 24 |
+
except requests.exceptions.RequestException as e:
|
| 25 |
+
print(f"An error occurred: {e}")
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
+
def download_file(url):
|
| 29 |
+
try:
|
| 30 |
+
response = requests.get(url.split("#")[0], stream=True)
|
| 31 |
+
response.raise_for_status()
|
| 32 |
+
print(url.split("#")[1])
|
| 33 |
+
with open(url.split("#")[1], 'wb') as file:
|
| 34 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 35 |
+
if chunk:
|
| 36 |
+
file.write(chunk)
|
| 37 |
+
print(f"Downloaded successfully: {url.split('#')[1]}")
|
| 38 |
+
except requests.exceptions.RequestException as e:
|
| 39 |
+
print(f"An error occurred: {e}")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def one_youtube(link):
|
| 43 |
+
|
| 44 |
+
# Fetch video ID
|
| 45 |
+
video_id_url = f"https://one-api.ir/youtube/?token={api_key}&action=getvideoid&link={link}"
|
| 46 |
+
video_data = fetch_data(video_id_url)
|
| 47 |
+
if not video_data:
|
| 48 |
+
return None, None
|
| 49 |
+
|
| 50 |
+
video_id = video_data["result"]
|
| 51 |
+
|
| 52 |
+
# Fetch video data
|
| 53 |
+
filter_option = "" # Replace with your filter option
|
| 54 |
+
video_data_url = f"https://youtube.one-api.ir/?token={api_key}&action=fullvideo&id={video_id}&filter={filter_option}"
|
| 55 |
+
video_data_2 = fetch_data(video_data_url)
|
| 56 |
+
if not video_data_2:
|
| 57 |
+
return None, None
|
| 58 |
+
|
| 59 |
+
formats_list = video_data_2["result"]["formats"]
|
| 60 |
+
file_name = video_data_2["result"]["title"]
|
| 61 |
+
video_name = f'{file_name}.mp4'
|
| 62 |
+
audio_name = f'{file_name}.mp3'
|
| 63 |
+
|
| 64 |
+
download_id = next((f["id"] for f in formats_list if f["format_note"] == "360p"), None)
|
| 65 |
+
audio_id = next((f["id"] for f in formats_list if f["format_note"] == "medium"), None)
|
| 66 |
+
if not download_id or not audio_id:
|
| 67 |
+
return None, None
|
| 68 |
+
|
| 69 |
+
# Fetch video and audio links
|
| 70 |
+
video_link_url = f"https://youtube.one-api.ir/?token={api_key}&action=download&id={download_id}"
|
| 71 |
+
audio_link_url = f"https://youtube.one-api.ir/?token={api_key}&action=download&id={audio_id}"
|
| 72 |
+
video_link_data = fetch_data(video_link_url)
|
| 73 |
+
audio_link_data = fetch_data(audio_link_url)
|
| 74 |
+
if not video_link_data or not audio_link_data:
|
| 75 |
+
return None, None
|
| 76 |
+
|
| 77 |
+
video_link = video_link_data["result"]["link"]
|
| 78 |
+
audio_link = audio_link_data["result"]["link"]
|
| 79 |
+
vid_str=video_link+"#"+video_name
|
| 80 |
+
audio_str=audio_link+"#"+audio_name
|
| 81 |
+
# Download video and audio files
|
| 82 |
+
download_file(vid_str)
|
| 83 |
+
download_file(audio_str)
|
| 84 |
+
|
| 85 |
+
return video_name, audio_name
|
| 86 |
+
|
| 87 |
+
|
| 88 |
# Define your functions here
|
| 89 |
def yt_download(url):
|
| 90 |
yt = YouTube(url)
|
|
|
|
| 177 |
def time_to_seconds(time_obj):
|
| 178 |
return time_obj.hours * 3600 + time_obj.minutes * 60 + time_obj.seconds + time_obj.milliseconds / 1000
|
| 179 |
|
| 180 |
+
def create_subtitle_clips(subtitles, videosize, fontsize=32, font='/content/Arial Unicode MS.ttf', color='yellow', debug=False):
|
| 181 |
subtitle_clips = []
|
| 182 |
for subtitle in subtitles:
|
| 183 |
start_time = time_to_seconds(subtitle.start)
|
|
|
|
| 186 |
video_width, video_height = videosize
|
| 187 |
reshaped_text = arabic_reshaper.reshape(subtitle.text)
|
| 188 |
bidi_text = get_display(reshaped_text)
|
| 189 |
+
text_clip = TextClip(bidi_text, fontsize=fontsize, font=font, color=color, bg_color='black', size=(video_width*3/4, None), method='label', align='ٍEast').set_start(start_time).set_duration(duration)
|
| 190 |
subtitle_x_position = 'center'
|
| 191 |
subtitle_y_position = video_height * 4 / 5
|
| 192 |
text_position = (subtitle_x_position, subtitle_y_position)
|
| 193 |
subtitle_clips.append(text_clip.set_position(text_position))
|
| 194 |
return subtitle_clips
|
| 195 |
|
| 196 |
+
def process_video(url):
|
| 197 |
+
|
| 198 |
+
input_video, input_audio = one_youtube(url)
|
| 199 |
input_video_name = input_video.replace(".mp4", "")
|
| 200 |
+
segments = transcribe(audio=input_audio)
|
|
|
|
| 201 |
language = "fa"
|
| 202 |
subtitle_file = generate_subtitle_file(language=language, segments=segments, input_video_name=input_video_name)
|
| 203 |
source_language = "en"
|
|
|
|
| 206 |
google_translate = translate_text(api_key, source_language, target_language, srt_string)
|
| 207 |
write_google(google_translate)
|
| 208 |
video = VideoFileClip(input_video)
|
| 209 |
+
audio = AudioFileClip(input_audio)
|
| 210 |
+
video = video.set_audio(audio)
|
| 211 |
+
print(video)
|
| 212 |
+
subtitles = pysrt.open("google_translate.srt", encoding="utf-8")
|
| 213 |
output_video_file = input_video_name + '_subtitled' + ".mp4"
|
| 214 |
subtitle_clips = create_subtitle_clips(subtitles, video.size)
|
| 215 |
final_video = CompositeVideoClip([video] + subtitle_clips)
|
| 216 |
final_video.write_videofile(output_video_file, codec="libx264", audio_codec="aac")
|
| 217 |
+
return google_translate, output_video_file
|
| 218 |
|
| 219 |
+
#def download_file(file_path):
|
| 220 |
+
# return gr.File.update(file_path)
|
| 221 |
|
| 222 |
+
iface = gr.Interface(fn=process_video, inputs=["text"], outputs="file")
|
| 223 |
|
| 224 |
+
iface.launch(debug=True)
|