Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import zipfile
|
3 |
+
import csv
|
4 |
+
import moviepy.editor as mp
|
5 |
+
from moviepy.editor import concatenate_videoclips
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
# Function to unzip the downloaded videos
|
9 |
+
def unzip_videos(zip_file, extract_dir):
|
10 |
+
with zipfile.ZipFile(zip_file.name, 'r') as zip_ref:
|
11 |
+
zip_ref.extractall(extract_dir)
|
12 |
+
return extract_dir
|
13 |
+
|
14 |
+
# Function to concatenate videos based on the CSV file
|
15 |
+
def concatenate_videos_from_csv(video_dir, csv_file, audio_file, music_file):
|
16 |
+
clips = []
|
17 |
+
with open(csv_file.name, 'r', encoding='utf-8') as file:
|
18 |
+
reader = csv.DictReader(file)
|
19 |
+
for row in reader:
|
20 |
+
word = row['word']
|
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 |
+
final_video = concatenate_videoclips(clips)
|
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 |
+
output_file = "final_output_video.mp4"
|
42 |
+
final_video.write_videofile(output_file)
|
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 |
+
return output_video
|
55 |
+
|
56 |
+
# Create Gradio UI
|
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 |
+
download_button.click(gradio_interface, [zip_file, csv_file, audio_file, music_file], output_video)
|
66 |
+
|
67 |
+
# Launch the Gradio interface
|
68 |
+
demo.launch()
|