Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,152 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
video.close()
|
| 16 |
-
return audio_path
|
| 17 |
except Exception as e:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
subtitle = srt.Subtitle(index=i+1,
|
| 29 |
-
start=timedelta(seconds=start_time),
|
| 30 |
-
end=timedelta(seconds=end_time),
|
| 31 |
-
content=content)
|
| 32 |
-
subtitles.append(subtitle)
|
| 33 |
-
with open(srt_file, 'w', encoding='utf-8') as f:
|
| 34 |
-
f.write(srt.compose(subtitles))
|
| 35 |
-
return srt_file
|
| 36 |
-
|
| 37 |
-
def process_video(video):
|
| 38 |
-
video_path = video
|
| 39 |
-
audio_path = extract_audio(video_path)
|
| 40 |
-
if audio_path.endswith('.mp3'):
|
| 41 |
-
processed_audio_path = transcribe_audio_to_srt(audio_path)
|
| 42 |
-
with open(processed_audio_path, "r") as f:
|
| 43 |
-
srt_content = f.read()
|
| 44 |
-
return srt_content
|
| 45 |
else:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
iface.launch()
|
|
|
|
| 1 |
+
from pytubefix import YouTube
|
| 2 |
+
from pytubefix.cli import on_progress
|
| 3 |
+
import time
|
| 4 |
+
import math
|
| 5 |
+
import gradio
|
| 6 |
+
import ffmpeg
|
| 7 |
+
from faster_whisper import WhisperModel
|
| 8 |
+
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.editor 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)
|
| 22 |
+
print(yt.title)
|
| 23 |
+
video_path = f"{yt.title}.mp4"
|
| 24 |
+
ys = yt.streams.get_highest_resolution()
|
| 25 |
+
print(ys)
|
| 26 |
+
ys.download()
|
| 27 |
+
return video_path, yt.title
|
| 28 |
+
|
| 29 |
+
def insta_download(shortcode, id_1):
|
| 30 |
+
url = "https://api.one-api.ir/instagram/v1/post/"
|
| 31 |
+
request_body = {"shortcode": shortcode, "id": id_1}
|
| 32 |
+
headers = {"one-api-token": api_key, "Content-Type": "application/json"}
|
| 33 |
+
response = requests.post(url, headers=headers, json=request_body)
|
| 34 |
+
if response.status_code == 200:
|
| 35 |
+
result = response.json()
|
| 36 |
+
return result['result']
|
| 37 |
+
else:
|
| 38 |
+
print(f"Error: {response.status_code}, {response.text}")
|
| 39 |
+
return None
|
| 40 |
+
|
| 41 |
+
def extract_audio(input_video_name):
|
| 42 |
+
extracted_audio = f"audio-{input_video_name}.wav"
|
| 43 |
+
stream = ffmpeg.input(input_video)
|
| 44 |
+
stream = ffmpeg.output(stream, extracted_audio)
|
| 45 |
+
ffmpeg.run(stream, overwrite_output=True)
|
| 46 |
+
return extracted_audio
|
| 47 |
+
|
| 48 |
+
def transcribe(audio):
|
| 49 |
+
model = WhisperModel("tiny")
|
| 50 |
+
segments, info = model.transcribe(audio)
|
| 51 |
+
segments = list(segments)
|
| 52 |
+
for segment in segments:
|
| 53 |
+
print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
|
| 54 |
+
return segments
|
| 55 |
+
|
| 56 |
+
def format_time(seconds):
|
| 57 |
+
hours = math.floor(seconds / 3600)
|
| 58 |
+
seconds %= 3600
|
| 59 |
+
minutes = math.floor(seconds / 60)
|
| 60 |
+
seconds %= 60
|
| 61 |
+
milliseconds = round((seconds - math.floor(seconds)) * 1000)
|
| 62 |
+
seconds = math.floor(seconds)
|
| 63 |
+
formatted_time = f"{hours:02d}:{minutes:02d}:{seconds:01d},{milliseconds:03d}"
|
| 64 |
+
return formatted_time
|
| 65 |
+
|
| 66 |
+
def generate_subtitle_file(language, segments, input_video_name):
|
| 67 |
+
subtitle_file = f"sub-{input_video_name}.{language}.srt"
|
| 68 |
+
text = ""
|
| 69 |
+
for index, segment in enumerate(segments):
|
| 70 |
+
segment_start = format_time(segment.start)
|
| 71 |
+
segment_end = format_time(segment.end)
|
| 72 |
+
text += f"{str(index+1)} \n"
|
| 73 |
+
text += f"{segment_start} --> {segment_end} \n"
|
| 74 |
+
text += f"{segment.text} \n"
|
| 75 |
+
text += "\n"
|
| 76 |
+
f = open(subtitle_file, "w", encoding='utf8')
|
| 77 |
+
f.write(text)
|
| 78 |
+
f.close()
|
| 79 |
+
return subtitle_file
|
| 80 |
+
|
| 81 |
+
def read_srt_file(file_path):
|
| 82 |
try:
|
| 83 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 84 |
+
srt_content = file.read()
|
| 85 |
+
return srt_content
|
| 86 |
+
except FileNotFoundError:
|
| 87 |
+
print(f"The file {file_path} was not found.")
|
|
|
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
+
print(f"An error occurred: {e}")
|
| 90 |
+
|
| 91 |
+
def translate_text(api_key, source_lang, target_lang, text):
|
| 92 |
+
url = "https://api.one-api.ir/translate/v1/google/"
|
| 93 |
+
request_body = {"source": source_lang, "target": target_lang, "text": text}
|
| 94 |
+
headers = {"one-api-token": api_key, "Content-Type": "application/json"}
|
| 95 |
+
response = requests.post(url, headers=headers, json=request_body)
|
| 96 |
+
if response.status_code == 200:
|
| 97 |
+
result = response.json()
|
| 98 |
+
return result['result']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
else:
|
| 100 |
+
print(f"Error: {response.status_code}, {response.text}")
|
| 101 |
+
return None
|
| 102 |
+
|
| 103 |
+
def write_google(google_translate):
|
| 104 |
+
google = "google_translate.srt"
|
| 105 |
+
with open(google, 'w', encoding="utf-8") as f:
|
| 106 |
+
f.write(google_translate)
|
| 107 |
+
|
| 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=24, font='/content/arial-unicode-ms.ttf', color='yellow', debug=False):
|
| 112 |
+
subtitle_clips = []
|
| 113 |
+
for subtitle in subtitles:
|
| 114 |
+
start_time = time_to_seconds(subtitle.start)
|
| 115 |
+
end_time = time_to_seconds(subtitle.end)
|
| 116 |
+
duration = end_time - start_time
|
| 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='West').set_start(start_time).set_duration(duration)
|
| 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, api_key):
|
| 128 |
+
input_video, title = yt_download(url)
|
| 129 |
+
input_video_name = input_video.replace(".mp4", "")
|
| 130 |
+
extracted_audio = extract_audio(input_video_name)
|
| 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"
|
| 135 |
+
target_language = "fa"
|
| 136 |
+
srt_string = read_srt_file(subtitle_file)
|
| 137 |
+
google_translate = translate_text(api_key, source_language, target_language, srt_string)
|
| 138 |
+
write_google(google_translate)
|
| 139 |
+
video = VideoFileClip(input_video)
|
| 140 |
+
subtitles = pysrt.open("/content/google_translate.srt", encoding="utf-8")
|
| 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 |
+
return gr.File.update(file_path)
|
| 149 |
+
|
| 150 |
+
iface = gr.Interface(fn=process_video, inputs=["text", "text"], outputs="file")
|
| 151 |
|
| 152 |
iface.launch()
|