Spaces:
Sleeping
Sleeping
| from moviepy import VideoFileClip | |
| import gradio as gr | |
| import requests | |
| import os | |
| from gradio_client import Client, handle_file | |
| 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 | |
| duration = audio_clip.duration | |
| print(f"Audio duration: {duration}") | |
| # 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, duration | |
| def download_video(url): | |
| response = requests.get(url, stream=True) | |
| response.raise_for_status() | |
| video_file = "video.mp4" | |
| with open(video_file, 'wb') as file: | |
| for chunk in response.iter_content(chunk_size=8192): | |
| if chunk: | |
| file.write(chunk) | |
| print("Video downloaded successfully!") | |
| return video_file | |
| def main(url, clip_type, parameters, progress=gr.Progress()): | |
| """ | |
| This function reports progress by yielding intermediate updates. | |
| Gradio watches these yields to update the progress bar. | |
| """ | |
| if clip_type == "dub": | |
| progress(0, desc="پردازش شروع شد") | |
| yield "پردازش شروع شد", None | |
| time.sleep(2) | |
| progress(5, desc="در حال دریافت ویدئو") | |
| yield "در حال دریافت ویدئو", None | |
| video = download_video(url) | |
| #progress(10, desc="استخراج صوت") | |
| #yield "استخراج صوت", None | |
| #mp3_file, duration = extract_audio(video) | |
| progress(35, desc="تبدیل متن به صوت") | |
| yield "تبدیل متن به صوت", None | |
| client = Client("rayesh/transcribe") | |
| srt_file = client.predict( | |
| mp3_file=handle_file(video), | |
| api_name="/transcribe" | |
| ) | |
| client.close() | |
| progress(55, desc="در حال ترجمه") | |
| yield "در حال ترجمه", None | |
| subtitle_file = translate(srt_file) | |
| output_video_file = dub(subtitle_file, video) | |
| progress(100, desc="Dubbing complete") | |
| yield "درحال پردازش ویدئو", output_video_file | |
| os.remove(subtitle_file) | |
| else: | |
| # Assume parameters is a comma-separated string: "color,font" | |
| color, font = parameters.split(",") | |
| progress(0, desc="پردازش شروع شد") | |
| yield "پردازش شروع شد", None | |
| progress(0.05, desc="در حال دریافت ویدئو") | |
| yield "در حال دریافت ویدئو", None | |
| video = download_video(url) | |
| progress(10, desc="استخراج صوت") | |
| yield "استخراج صوت", None | |
| mp3_file, duration = extract_audio(video) | |
| progress(0.35, desc="تبدیل متن به صوت") | |
| yield "تبدیل متن به صوت", None | |
| client = Client("rayesh/transcribe") | |
| srt_file = client.predict( | |
| mp3_file=handle_file(mp3_file), | |
| api_name="/transcribe" | |
| ) | |
| client.close() | |
| progress(0.55, desc="در حال ترجمه") | |
| yield "در حال ترجمه", None | |
| client = Client("rayesh/translate") | |
| subtitle_file = client.predict( | |
| file=handle_file(srt_file), | |
| max_chars=3000, | |
| api_name="/translate" | |
| ) | |
| client.close() | |
| progress(1.0, desc="Video editing complete") | |
| yield "درحال پردازش ویدئو", None | |
| client = Client("SPACERUNNER99/video_edite") | |
| output_video_file = client.predict( | |
| srt=handle_file(subtitle_file), | |
| input_video={"video":handle_file(video)}, | |
| color=color, | |
| font=font, | |
| input_audio=handle_file(mp3_file), | |
| api_name="/video_edit" | |
| ) | |
| client.close() | |
| output_video_file = output_video_file['video'] | |
| yield "درحال پردازش ویدئو", output_video_file | |
| with gr.Blocks() as demo: | |
| gr.Markdown("Start typing below and then click **Run** to see the progress and final output.") | |
| with gr.Column(): | |
| progress_output = gr.Textbox(label="Progress", visible=False) | |
| video_file_input = gr.Text(label="Video URL") | |
| clip_type = gr.Dropdown(["dub", "sub"], label="Clip Type") | |
| parameters = gr.Text(label="Additional Parameters (for subtitles: color,font)") | |
| btn = gr.Button("Create") | |
| video_file_output = gr.Video(label="Result Video") | |
| btn.click( | |
| fn=main, | |
| inputs=[video_file_input, clip_type, parameters], | |
| outputs=[progress_output, video_file_output], | |
| ) | |
| demo.launch(debug=True) |