SPACERUNNER99 commited on
Commit
a3cd4b3
·
verified ·
1 Parent(s): 67ac112

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -57
app.py CHANGED
@@ -1,5 +1,5 @@
1
  from transcribe import transcribe
2
- from moviepy import *
3
  from translate import translate
4
  from edite_video import video_edit
5
  import gradio as gr
@@ -17,7 +17,7 @@ def extract_audio(input_video_name):
17
  # Extract the audio from the video clip
18
  audio_clip = video_clip.audio
19
  duration = audio_clip.duration
20
- print(duration)
21
  # Write the audio to a separate file
22
  audio_clip.write_audiofile(mp3_file)
23
 
@@ -28,68 +28,91 @@ def extract_audio(input_video_name):
28
  print("Audio extraction successful!")
29
  return mp3_file, duration
30
 
31
- def download_video(url):
32
 
 
33
  response = requests.get(url, stream=True)
34
  response.raise_for_status()
35
- with open("video.mp4", 'wb') as file:
 
36
  for chunk in response.iter_content(chunk_size=8192):
37
  if chunk:
38
  file.write(chunk)
39
- file.close()
40
- print(f"Downloaded successfully")
41
- return "video.mp4"
42
-
43
- def main(url, clip_type, parameters, progress=gr.Progress()):
44
-
45
- #if clip_type == "dub":
46
- #progress(0, desc="Starting")
47
- #video = download_video(url)
48
- #progress(5, desc="downloaded")
49
- #mp3_file, duration = extract_audio(video)
50
- #progress(10, desc="extract audio")
51
- #srt_list = transcribe(mp3_file)
52
- #progress(35, desc="transcribe")
53
- #subtitle_file = translate(srt_list)
54
- #progress(55, desc="translate")
55
- #output_video_file = dub(subtitle_file, video)
56
- #progress(100, desc="finish")
57
- #os.remove(subtitle_file)
58
- #else:
59
- color, font = parameters.split(",")
60
- progress(0, desc="Starting")
61
- video = download_video(url)
62
- progress(5, desc="downloaded")
63
- mp3_file, duration = extract_audio(video)
64
- progress(10, desc="extract audio")
65
- srt_list = transcribe(mp3_file)
66
- progress(35, desc="transcribe")
67
- subtitle_file = translate(srt_list)
68
- progress(55, desc="translate")
69
- print(parameters)
70
- output_video_file = video_edit(subtitle_file, video, color, font, input_audio= "audio.mp3")
71
- progress(100, desc="finish")
72
- os.remove(subtitle_file)
73
- return output_video_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  with gr.Blocks() as demo:
76
- gr.Markdown("Start typing below and then click **Run** to see the output.")
77
  with gr.Column():
78
- video_file_input = gr.Text(label="Upload Video url")
 
79
  clip_type = gr.Dropdown(["dub", "sub"], label="Clip Type")
80
- parameters = gr.Text()
81
- btn = gr.Button("create")
82
- video_file_output = gr.Video(label="result: ")
83
- btn.click(fn=main, inputs=[video_file_input, clip_type, parameters], outputs=video_file_output)
84
- """ with gr.Row():
85
- vid_out = gr.Video()
86
- srt_file = gr.File()
87
- btn2 = gr.Button("transcribe")
88
- gr.on(
89
- triggers=[btn2.click],
90
- fn=write_google,
91
- inputs=out,
92
- ).then(video_edit, [out, video_path_output, audio_path_output], outputs=[vid_out, srt_file])"""
93
-
94
-
95
- demo.launch(debug=True)
 
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
 
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
 
 
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)