Spaces:
Sleeping
Sleeping
File size: 6,404 Bytes
b4faf4a |
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 |
import gradio as gr
from yt_dlp import YoutubeDL
from moviepy.editor import AudioFileClip, VideoFileClip
import speech_recognition as sr
from deep_translator import GoogleTranslator
from fpdf import FPDF
from docx import Document
import gradio as gr
import os
def download_audio(video_url):
try:
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': 'audio.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with YoutubeDL(ydl_opts) as ydl:
ydl.download([video_url])
audio_file = 'audio.mp3'
audio = AudioFileClip(audio_file)
audio.write_audiofile('audio.wav')
# Clean up temporary audio file
os.remove(audio_file)
return 'audio.wav'
except Exception as e:
return f"Error downloading audio: {str(e)}"
def split_audio(audio_file, chunk_length=60):
try:
audio = AudioFileClip(audio_file)
duration = int(audio.duration)
chunks = []
for i in range(0, duration, chunk_length):
chunk = audio.subclip(i, min(i + chunk_length, duration))
chunk_file = f'audio_chunk_{i}.wav'
chunk.write_audiofile(chunk_file)
chunks.append(chunk_file)
return chunks
except Exception as e:
return f"Error splitting audio: {str(e)}"
def generate_subtitles(audio_file):
try:
recognizer = sr.Recognizer()
audio_chunks = split_audio(audio_file)
if isinstance(audio_chunks, str) and "Error" in audio_chunks:
return audio_chunks
subtitles = ""
for chunk_file in audio_chunks:
with sr.AudioFile(chunk_file) as source:
audio_content = recognizer.record(source)
try:
chunk_subtitles = recognizer.recognize_google(audio_content)
subtitles += chunk_subtitles + " "
except sr.RequestError as e:
print(f"API unavailable: {str(e)}")
continue
except sr.UnknownValueError:
print(f"Unable to recognize speech in chunk {chunk_file}")
continue
finally:
os.remove(chunk_file)
return subtitles.strip()
except Exception as e:
return f"Error generating subtitles: {str(e)}"
def translate_subtitles(subtitles, target_language):
try:
translated = GoogleTranslator(source='auto', target=target_language).translate(subtitles)
return translated
except Exception as e:
return f"Error translating subtitles: {str(e)}"
def save_subtitles_as_pdf(subtitles, filename='subtitles.pdf'):
try:
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.multi_cell(0, 10, subtitles)
pdf.output(filename)
return filename
except Exception as e:
return f"Error saving PDF: {str(e)}"
def save_subtitles_as_doc(subtitles, filename='subtitles.docx'):
try:
doc = Document()
doc.add_paragraph(subtitles)
doc.save(filename)
return filename
except Exception as e:
return f"Error saving DOC: {str(e)}"
def download_video(video_url):
try:
ydl_opts = {
'format': 'bestvideo+bestaudio/best',
'outtmpl': 'video.%(ext)s',
'postprocessors': [{
'key': 'FFmpegVideoConvertor',
'preferedformat': 'mp4', # Converts to MP4 format
}],
}
with YoutubeDL(ydl_opts) as ydl:
ydl.download([video_url])
# Determine the video file extension
video_files = [f for f in os.listdir() if f.startswith('video.') and os.path.isfile(f)]
if video_files:
# Rename the file to a standard name if multiple files are found
video_file = video_files[0]
mp4_file = 'video.mp4'
if not video_file.endswith('.mp4'):
# Rename the file to ensure it's in MP4 format
os.rename(video_file, mp4_file)
return mp4_file
else:
return "Error: No video file found."
except Exception as e:
return f"Error downloading video: {str(e)}"
def process_video(video_url, target_language):
audio_file = download_audio(video_url)
if "Error" in audio_file:
return audio_file, None, None, None, None
subtitles = generate_subtitles(audio_file)
if "Error" in subtitles:
return subtitles, None, None, None, None
translated_subtitles = translate_subtitles(subtitles, target_language)
if "Error" in translated_subtitles:
return translated_subtitles, None, None, None, None
pdf_file = save_subtitles_as_pdf(translated_subtitles)
if "Error" in pdf_file:
return pdf_file, None, None, None, None
doc_file = save_subtitles_as_doc(translated_subtitles)
if "Error" in doc_file:
return doc_file, None, None, None, None
video_file = download_video(video_url)
if "Error" in video_file:
return video_file, None, None, None, None
return translated_subtitles, pdf_file, doc_file, audio_file, video_file
# Fetch supported languages from deep-translator
supported_languages = GoogleTranslator().get_supported_languages(as_dict=True)
language_choices = list(supported_languages.values())
def gradio_process(video_url, target_language):
translated_subtitles, pdf_file, doc_file, audio_file, video_file = process_video(video_url, target_language)
return translated_subtitles, pdf_file, doc_file, audio_file, video_file
gr.Interface(
fn=gradio_process,
inputs=[
gr.Textbox(label="Video URL"),
gr.Dropdown(label="Target Language", choices=language_choices)
],
outputs=[
gr.Textbox(label="Translated Subtitles"),
gr.File(label="Download PDF"),
gr.File(label="Download DOC"),
gr.File(label="Download AUDIO"),
gr.File(label="Download VIDEO")
]
).launch()
|