Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,58 @@
|
|
|
|
1 |
import os
|
2 |
import zipfile
|
3 |
-
import
|
4 |
import moviepy.editor as mp
|
5 |
-
from moviepy.editor import concatenate_videoclips
|
6 |
-
import gradio as gr
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
with open(csv_file.name, 'r'
|
18 |
-
reader = csv.DictReader(
|
19 |
for row in reader:
|
20 |
-
|
21 |
-
video_path = os.path.join(video_dir, f"{word}.mp4")
|
22 |
-
if os.path.exists(video_path):
|
23 |
-
clip = mp.VideoFileClip(video_path)
|
24 |
-
clips.append(clip)
|
25 |
-
|
26 |
-
if not clips:
|
27 |
-
raise ValueError("No videos were found that match the CSV file.")
|
28 |
|
29 |
-
|
|
|
|
|
30 |
|
31 |
-
# Add music if provided
|
32 |
-
if music_file:
|
33 |
music = mp.AudioFileClip(music_file.name)
|
34 |
final_video = final_video.set_audio(music)
|
35 |
|
36 |
-
# Add narration audio if provided
|
37 |
-
if audio_file:
|
38 |
narration = mp.AudioFileClip(audio_file.name)
|
39 |
final_video = final_video.set_audio(narration)
|
40 |
|
41 |
-
|
42 |
-
final_video.
|
43 |
-
|
44 |
-
return output_file
|
45 |
-
|
46 |
-
# Gradio Interface
|
47 |
-
def gradio_interface(zip_file, csv_file, audio_file, music_file):
|
48 |
-
extract_dir = "extracted_videos"
|
49 |
-
os.makedirs(extract_dir, exist_ok=True)
|
50 |
-
|
51 |
-
video_dir = unzip_videos(zip_file, extract_dir)
|
52 |
-
output_video = concatenate_videos_from_csv(video_dir, csv_file, audio_file, music_file)
|
53 |
|
54 |
-
|
|
|
55 |
|
56 |
-
|
57 |
-
with gr.Blocks() as demo:
|
58 |
-
zip_file = gr.File(label="Upload Video ZIP File")
|
59 |
-
csv_file = gr.File(label="Upload CSV File")
|
60 |
-
audio_file = gr.File(label="Upload Narration Audio File (Optional)", optional=True)
|
61 |
-
music_file = gr.File(label="Upload Background Music File (Optional)", optional=True)
|
62 |
-
download_button = gr.Button("Concatenate Videos and Add Audio")
|
63 |
-
output_video = gr.File(label="Final Video File", interactive=False)
|
64 |
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
# Launch the Gradio interface
|
68 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
import os
|
3 |
import zipfile
|
4 |
+
import shutil
|
5 |
import moviepy.editor as mp
|
|
|
|
|
6 |
|
7 |
+
def process_files(audio_file, video_zip, music_file, csv_file):
|
8 |
+
# Unzip the video files
|
9 |
+
video_folder = "videos"
|
10 |
+
with zipfile.ZipFile(video_zip.name, 'r') as zip_ref:
|
11 |
+
zip_ref.extractall(video_folder)
|
12 |
+
|
13 |
+
# Read the CSV file and get the order of videos
|
14 |
+
import csv
|
15 |
+
video_order = []
|
16 |
+
with open(csv_file.name, 'r') as csvfile:
|
17 |
+
reader = csv.DictReader(csvfile)
|
18 |
for row in reader:
|
19 |
+
video_order.append(os.path.join(video_folder, f"{row['word']}.mp4"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
# Concatenate the videos based on the CSV order
|
22 |
+
video_clips = [mp.VideoFileClip(video) for video in video_order]
|
23 |
+
final_video = mp.concatenate_videoclips(video_clips, method="compose")
|
24 |
|
25 |
+
# Add music to the final video if provided
|
26 |
+
if music_file is not None:
|
27 |
music = mp.AudioFileClip(music_file.name)
|
28 |
final_video = final_video.set_audio(music)
|
29 |
|
30 |
+
# Add narration audio to the final video if provided
|
31 |
+
if audio_file is not None:
|
32 |
narration = mp.AudioFileClip(audio_file.name)
|
33 |
final_video = final_video.set_audio(narration)
|
34 |
|
35 |
+
# Save the final video
|
36 |
+
output_video_path = "final_video.mp4"
|
37 |
+
final_video.write_videofile(output_video_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
# Cleanup extracted video files
|
40 |
+
shutil.rmtree(video_folder)
|
41 |
|
42 |
+
return output_video_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
# Define the Gradio interface
|
45 |
+
interface = gr.Interface(
|
46 |
+
fn=process_files,
|
47 |
+
inputs=[
|
48 |
+
gr.File(label="Upload Narration Audio File (Optional)"), # Updated
|
49 |
+
gr.File(label="Upload Video Zip File"),
|
50 |
+
gr.File(label="Upload Music File"),
|
51 |
+
gr.File(label="Upload CSV File")
|
52 |
+
],
|
53 |
+
outputs=gr.File(label="Download Final Video"),
|
54 |
+
title="Video Concatenation Tool"
|
55 |
+
)
|
56 |
|
57 |
# Launch the Gradio interface
|
58 |
+
interface.launch()
|