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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Untitled1.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1OxX4WwJL-ZQPL79F5LBrHOxsXcePSiWN
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
#install gradio
|
| 12 |
+
|
| 13 |
+
#sudo apt update && sudo apt install ffmpeg
|
| 14 |
+
|
| 15 |
+
#pip install ffmpeg
|
| 16 |
+
|
| 17 |
+
#pip install git+https://github.com/openai/whisper.git
|
| 18 |
+
#pip install pytube transformers moviepy TTS youtube_transcript_api pydub SentencePiece pysubs2
|
| 19 |
+
|
| 20 |
+
import os
|
| 21 |
+
import gradio as gr
|
| 22 |
+
import re
|
| 23 |
+
import nltk
|
| 24 |
+
from pytube import YouTube
|
| 25 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 26 |
+
from moviepy.editor import VideoFileClip, concatenate_audioclips, AudioFileClip
|
| 27 |
+
from whisper import load_model
|
| 28 |
+
from TTS.api import TTS
|
| 29 |
+
from pydub import AudioSegment, silence
|
| 30 |
+
import pysubs2
|
| 31 |
+
import subprocess
|
| 32 |
+
|
| 33 |
+
nltk.download('punkt')
|
| 34 |
+
model_name = 'Helsinki-NLP/opus-mt-en-fr'
|
| 35 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 36 |
+
model = MarianMTModel.from_pretrained(model_name)
|
| 37 |
+
tts = TTS(model_name="tts_models/fr/css10/vits")
|
| 38 |
+
whisper_model = load_model("base")
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
save_path = "videos6"
|
| 42 |
+
os.makedirs(save_path, exist_ok=True)
|
| 43 |
+
audio_folder = os.path.join(save_path, "audio")
|
| 44 |
+
os.makedirs(audio_folder, exist_ok=True)
|
| 45 |
+
tts_audio_folder = os.path.join(save_path, "tts_audio")
|
| 46 |
+
os.makedirs(tts_audio_folder, exist_ok=True)
|
| 47 |
+
|
| 48 |
+
def cleaned_video(video_name):
|
| 49 |
+
return re.sub(r'[\\/*?:"<>|]', "", video_name)
|
| 50 |
+
|
| 51 |
+
def read_subtitles(subtitles_file):
|
| 52 |
+
with open(subtitles_file, 'r', encoding='utf-8') as file:
|
| 53 |
+
return file.read()
|
| 54 |
+
|
| 55 |
+
def translate(text):
|
| 56 |
+
sentences = nltk.tokenize.sent_tokenize(text)
|
| 57 |
+
translations = []
|
| 58 |
+
for sentence in sentences:
|
| 59 |
+
batch = tokenizer(sentence, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
| 60 |
+
gen = model.generate(**batch)
|
| 61 |
+
translation = tokenizer.batch_decode(gen, skip_special_tokens=True)
|
| 62 |
+
translations.append(translation[0])
|
| 63 |
+
return ' '.join(translations)
|
| 64 |
+
|
| 65 |
+
def generate_tts_audio(text, start, end, tts_audio_path):
|
| 66 |
+
tts.tts_to_file(text=text, file_path=tts_audio_path)
|
| 67 |
+
tts_audio = AudioSegment.from_mp3(tts_audio_path)
|
| 68 |
+
expected_duration = (end - start) * 1000
|
| 69 |
+
actual_duration = len(tts_audio)
|
| 70 |
+
if actual_duration < expected_duration:
|
| 71 |
+
silence_duration = expected_duration - actual_duration
|
| 72 |
+
silence_audio = AudioSegment.silent(duration=silence_duration)
|
| 73 |
+
tts_audio += silence_audio
|
| 74 |
+
tts_audio.export(tts_audio_path, format='wav')
|
| 75 |
+
return True
|
| 76 |
+
|
| 77 |
+
def create_subtitles(segments, subtitles_file):
|
| 78 |
+
subs = pysubs2.SSAFile()
|
| 79 |
+
for start, end, text in segments:
|
| 80 |
+
start_ms = int(start * 1000)
|
| 81 |
+
end_ms = int(end * 1000)
|
| 82 |
+
subs.append(pysubs2.SSAEvent(start=start_ms, end=end_ms, text=text))
|
| 83 |
+
subs.save(subtitles_file)
|
| 84 |
+
|
| 85 |
+
def embed_subtitles(video_path, subtitles_path, output_path):
|
| 86 |
+
command = [
|
| 87 |
+
'ffmpeg',
|
| 88 |
+
'-i', video_path,
|
| 89 |
+
'-vf', f"subtitles={subtitles_path}",
|
| 90 |
+
'-c:a', 'copy',
|
| 91 |
+
output_path
|
| 92 |
+
]
|
| 93 |
+
subprocess.run(command, check=True)
|
| 94 |
+
return
|
| 95 |
+
|
| 96 |
+
def process_video(url):
|
| 97 |
+
yt = YouTube(url)
|
| 98 |
+
video_id = yt.video_id
|
| 99 |
+
yt_title_cleaned = cleaned_video(yt.title)
|
| 100 |
+
video_stream = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
|
| 101 |
+
|
| 102 |
+
if not video_stream:
|
| 103 |
+
print("No suitable video stream found.")
|
| 104 |
+
return None
|
| 105 |
+
|
| 106 |
+
video_path = os.path.join(save_path, yt_title_cleaned + ".mp4")
|
| 107 |
+
video_stream.download(output_path=save_path, filename=yt_title_cleaned + ".mp4")
|
| 108 |
+
|
| 109 |
+
video_clip = VideoFileClip(video_path)
|
| 110 |
+
audio_path = os.path.join(audio_folder, yt_title_cleaned + ".mp3")
|
| 111 |
+
video_clip.audio.write_audiofile(audio_path)
|
| 112 |
+
|
| 113 |
+
result = whisper_model.transcribe(audio_path)
|
| 114 |
+
segments = []
|
| 115 |
+
for segment in result["segments"]:
|
| 116 |
+
start_time, end_time, text = segment["start"], segment["end"], segment["text"]
|
| 117 |
+
segments.append((start_time, end_time, text))
|
| 118 |
+
|
| 119 |
+
translated_segments = []
|
| 120 |
+
tts_clips = []
|
| 121 |
+
for start, end, text in segments:
|
| 122 |
+
translated_text = translate(text)
|
| 123 |
+
translated_segments.append((start, end, translated_text))
|
| 124 |
+
|
| 125 |
+
tts_audio_path = os.path.join(tts_audio_folder, f"tts_{start}_{end}.wav")
|
| 126 |
+
generate_tts_audio(translated_text, start, end, tts_audio_path)
|
| 127 |
+
|
| 128 |
+
tts_clip = AudioFileClip(tts_audio_path).subclip(0, end - start)
|
| 129 |
+
tts_clips.append(tts_clip)
|
| 130 |
+
|
| 131 |
+
combined_tts_audio = concatenate_audioclips(tts_clips)
|
| 132 |
+
final_video = video_clip.set_audio(combined_tts_audio)
|
| 133 |
+
final_video_path = os.path.join(save_path, yt_title_cleaned + "_translated.mp4")
|
| 134 |
+
final_video.write_videofile(final_video_path)
|
| 135 |
+
|
| 136 |
+
# Save the original and translated subtitles
|
| 137 |
+
original_subtitles_file = os.path.join(save_path, yt_title_cleaned + "_original.srt")
|
| 138 |
+
create_subtitles(segments, original_subtitles_file)
|
| 139 |
+
|
| 140 |
+
translated_subtitles_file = os.path.join(save_path, yt_title_cleaned + "_translated.srt")
|
| 141 |
+
create_subtitles(translated_segments, translated_subtitles_file)
|
| 142 |
+
|
| 143 |
+
# Read and return subtitles text
|
| 144 |
+
original_subtitles_text = read_subtitles(original_subtitles_file)
|
| 145 |
+
translated_subtitles_text = read_subtitles(translated_subtitles_file)
|
| 146 |
+
|
| 147 |
+
return final_video_path, original_subtitles_text, translated_subtitles_text
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
#url = 'https://youtu.be/AlhELuRMJ_s?si=r2la5DQlOU49QDPW'
|
| 151 |
+
|
| 152 |
+
#processed_video_path = process_video(url)
|
| 153 |
+
#if processed_video_path:
|
| 154 |
+
#print(f"Processed video saved at {processed_video_path}")
|
| 155 |
+
#else:
|
| 156 |
+
#print("Failed to process video.")
|
| 157 |
+
|
| 158 |
+
with gr.Blocks() as demo:
|
| 159 |
+
with gr.Row():
|
| 160 |
+
text_box = gr.Textbox(label="Enter YouTube Video Link", placeholder="Text box for link")
|
| 161 |
+
submit_btn = gr.Button("Submit")
|
| 162 |
+
|
| 163 |
+
video = gr.Video()
|
| 164 |
+
|
| 165 |
+
with gr.Row():
|
| 166 |
+
original_subs_output = gr.Textbox(label="Original Subs", placeholder="Original Subs", interactive=False)
|
| 167 |
+
translated_subs_output = gr.Textbox(label="Translated Subs", placeholder="Translated Subs", interactive=False)
|
| 168 |
+
|
| 169 |
+
submit_btn.click(fn=process_video, inputs=text_box, outputs=[video, original_subs_output, translated_subs_output])
|
| 170 |
+
|
| 171 |
+
demo.launch()
|
| 172 |
+
|
| 173 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pytube
|
| 2 |
+
transformers
|
| 3 |
+
moviepy
|
| 4 |
+
TTS
|
| 5 |
+
youtube_transcript_api
|
| 6 |
+
pydub
|
| 7 |
+
SentencePiece
|
| 8 |
+
pysubs2
|
| 9 |
+
gradio
|
| 10 |
+
ffmpeg
|
| 11 |
+
git+https://github.com/openai/whisper.git
|