SPACERUNNER99 commited on
Commit
2a3df7f
·
verified ·
1 Parent(s): 1d2716f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -0
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transcribe import transcribe
2
+ from moviepy import VideoFileClip
3
+ from translate import translate
4
+ from edite_video import video_edit
5
+ import gradio as gr
6
+ import requests
7
+ import os
8
+ from dub import dub
9
+
10
+
11
+ def extract_audio(input_video_name):
12
+ # Define the input video file and output audio file
13
+ mp3_file = "audio.mp3"
14
+ # Load the video clip
15
+ video_clip = VideoFileClip(input_video_name)
16
+
17
+ # Extract the audio from the video clip
18
+ audio_clip = video_clip.audio
19
+ duration = audio_clip.duration
20
+ print(f"Audio duration: {duration}")
21
+ # Write the audio to a separate file
22
+ audio_clip.write_audiofile(mp3_file)
23
+
24
+ # Close the video and audio clips
25
+ audio_clip.close()
26
+ video_clip.close()
27
+
28
+ print("Audio extraction successful!")
29
+ return mp3_file, duration
30
+
31
+
32
+ def download_video(url):
33
+ response = requests.get(url, stream=True)
34
+ response.raise_for_status()
35
+ video_file = "video.mp4"
36
+ with open(video_file, 'wb') as file:
37
+ for chunk in response.iter_content(chunk_size=8192):
38
+ if chunk:
39
+ file.write(chunk)
40
+ print("Video downloaded successfully!")
41
+ return video_file
42
+
43
+
44
+ def main(url, clip_type, parameters, progress=gr.Progress()):
45
+ """
46
+ This function reports progress by yielding intermediate updates.
47
+ Gradio watches these yields to update the progress bar.
48
+ """
49
+ if clip_type == "dub":
50
+ progress(0, desc="پردازش شروع شد")
51
+ yield "پردازش شروع شد", None
52
+
53
+ video = download_video(url)
54
+ progress(5, desc="در حال دریافت ویدئو")
55
+ yield "در حال دریافت ویدئو", None
56
+
57
+ mp3_file, duration = extract_audio(video)
58
+ progress(10, desc="استخراج صوت")
59
+ yield "استخراج صوت", None
60
+
61
+ srt_list = transcribe(mp3_file)
62
+ progress(35, desc="تبدیل متن به صوت")
63
+ yield "تبدیل متن به صوت", None
64
+
65
+ subtitle_file = translate(srt_list)
66
+ progress(55, desc="در حال ترجمه")
67
+ yield "در حال ترجمه", None
68
+
69
+ output_video_file = dub(subtitle_file, video)
70
+ progress(100, desc="Dubbing complete")
71
+ yield "درحال پردازش ویدئو", output_video_file
72
+ os.remove(subtitle_file)
73
+
74
+ else:
75
+ # Assume parameters is a comma-separated string: "color,font"
76
+ color, font = parameters.split(",")
77
+ progress(0, desc="پردازش شروع شد")
78
+ yield "پردازش شروع شد", None
79
+
80
+ video = download_video(url)
81
+ progress(5, desc="در حال دریافت ویدئو")
82
+ yield "در حال دریافت ویدئو", None
83
+
84
+ mp3_file, duration = extract_audio(video)
85
+ progress(10, desc="استخراج صوت")
86
+ yield "استخراج صوت", None
87
+
88
+ srt_list = transcribe(mp3_file)
89
+ progress(35, desc="تبدیل متن به صوت")
90
+ yield "تبدیل متن به صوت", None
91
+
92
+ subtitle_file = translate(srt_list)
93
+ progress(55, desc="در حال ترجمه")
94
+ yield "در حال ترجمه", None
95
+
96
+
97
+ output_video_file = video_edit(subtitle_file, video, color, font, input_audio="audio.mp3")
98
+ progress(100, desc="Video editing complete")
99
+ yield "درحال پردازش ویدئو", output_video_file
100
+ os.remove(subtitle_file)
101
+
102
+
103
+ with gr.Blocks() as demo:
104
+ gr.Markdown("Start typing below and then click **Run** to see the progress and final output.")
105
+ with gr.Column():
106
+ progress_output = gr.Textbox(label="Progress", visible=False)
107
+ video_file_input = gr.Text(label="Video URL")
108
+ clip_type = gr.Dropdown(["dub", "sub"], label="Clip Type")
109
+ parameters = gr.Text(label="Additional Parameters (for subtitles: color,font)")
110
+ btn = gr.Button("Create")
111
+ video_file_output = gr.Video(label="Result Video")
112
+ btn.click(
113
+ fn=main,
114
+ inputs=[video_file_input, clip_type, parameters],
115
+ outputs=[progress_output, video_file_output],
116
+ )
117
+
118
+ demo.launch(debug=True)