Update app.py
Browse files
app.py
CHANGED
|
@@ -2,68 +2,54 @@ import gradio as gr
|
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
import zipfile
|
| 5 |
-
import shutil
|
| 6 |
|
| 7 |
-
def process_videos(
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 10 |
output_paths = []
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
f.write(logo_file.read())
|
| 17 |
-
with open(outro_path, "wb") as f:
|
| 18 |
-
f.write(outro_file.read())
|
| 19 |
-
|
| 20 |
-
for video in video_file_list:
|
| 21 |
-
input_path = video.name
|
| 22 |
-
with open(input_path, "wb") as f:
|
| 23 |
-
f.write(video.read())
|
| 24 |
-
|
| 25 |
-
watermarked_path = os.path.join("outputs", f"watermarked_{os.path.basename(input_path)}")
|
| 26 |
-
final_path = os.path.join("outputs", f"final_{os.path.basename(input_path)}")
|
| 27 |
|
| 28 |
# Add watermark using FFmpeg
|
| 29 |
subprocess.run(
|
| 30 |
-
f"ffmpeg -y -i {
|
| 31 |
-
shell=True
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
-
# Add outro
|
| 35 |
subprocess.run(
|
| 36 |
-
f"ffmpeg -y -i {watermarked_path} -i {outro_path} -filter_complex
|
| 37 |
-
shell=True
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
output_paths.append(final_path)
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
os.remove(input_path)
|
| 44 |
os.remove(watermarked_path)
|
| 45 |
|
| 46 |
-
#
|
| 47 |
zip_filename = "processed_videos.zip"
|
| 48 |
with zipfile.ZipFile(zip_filename, 'w') as zipf:
|
| 49 |
for filepath in output_paths:
|
| 50 |
-
zipf.write(filepath)
|
| 51 |
os.remove(filepath) # Remove after adding to ZIP
|
| 52 |
|
| 53 |
-
# Cleanup logo and outro files
|
| 54 |
-
os.remove(logo_path)
|
| 55 |
-
os.remove(outro_path)
|
| 56 |
-
|
| 57 |
return zip_filename
|
| 58 |
|
| 59 |
demo = gr.Interface(
|
| 60 |
fn=process_videos,
|
| 61 |
inputs=[
|
| 62 |
-
gr.Files(label="Upload Videos", file_types=[".mp4"], type="
|
| 63 |
-
gr.File(label="Upload Logo (PNG)", file_types=[".png"]),
|
| 64 |
-
gr.File(label="Upload Outro Video (MP4)", file_types=[".mp4"])
|
| 65 |
],
|
| 66 |
-
outputs=gr.File(label="Download Processed Videos
|
| 67 |
title="Bulk Video Watermark and Outro Adder",
|
| 68 |
description="Upload multiple videos, a logo image, and an outro video to add watermark and append outro in bulk."
|
| 69 |
)
|
|
|
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
import zipfile
|
|
|
|
| 5 |
|
| 6 |
+
def process_videos(video_filepaths, logo_path, outro_path):
|
| 7 |
+
output_dir = "outputs"
|
| 8 |
+
if not os.path.exists(output_dir):
|
| 9 |
+
os.makedirs(output_dir)
|
| 10 |
output_paths = []
|
| 11 |
|
| 12 |
+
for video_path in video_filepaths:
|
| 13 |
+
basename = os.path.basename(video_path)
|
| 14 |
+
watermarked_path = os.path.join(output_dir, f"watermarked_{basename}")
|
| 15 |
+
final_path = os.path.join(output_dir, f"final_{basename}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Add watermark using FFmpeg
|
| 18 |
subprocess.run(
|
| 19 |
+
f"ffmpeg -y -i \"{video_path}\" -i \"{logo_path}\" -filter_complex \"overlay=10:10\" -codec:a copy \"{watermarked_path}\"",
|
| 20 |
+
shell=True,
|
| 21 |
+
check=True
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Add outro by concatenating video and outro
|
| 25 |
subprocess.run(
|
| 26 |
+
f"ffmpeg -y -i \"{watermarked_path}\" -i \"{outro_path}\" -filter_complex \"[0:v:0][0:a:0][1:v:0][1:a:0]concat=n=2:v=1:a=1[outv][outa]\" -map \"[outv]\" -map \"[outa]\" \"{final_path}\"",
|
| 27 |
+
shell=True,
|
| 28 |
+
check=True
|
| 29 |
)
|
| 30 |
|
| 31 |
output_paths.append(final_path)
|
| 32 |
|
| 33 |
+
# Clean intermediate watermarked file
|
|
|
|
| 34 |
os.remove(watermarked_path)
|
| 35 |
|
| 36 |
+
# Zip all processed videos
|
| 37 |
zip_filename = "processed_videos.zip"
|
| 38 |
with zipfile.ZipFile(zip_filename, 'w') as zipf:
|
| 39 |
for filepath in output_paths:
|
| 40 |
+
zipf.write(filepath, os.path.basename(filepath))
|
| 41 |
os.remove(filepath) # Remove after adding to ZIP
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return zip_filename
|
| 44 |
|
| 45 |
demo = gr.Interface(
|
| 46 |
fn=process_videos,
|
| 47 |
inputs=[
|
| 48 |
+
gr.Files(label="Upload Videos (MP4)", file_types=[".mp4"], type="filepaths"),
|
| 49 |
+
gr.File(label="Upload Logo (PNG)", file_types=[".png"], type="filepath"),
|
| 50 |
+
gr.File(label="Upload Outro Video (MP4)", file_types=[".mp4"], type="filepath")
|
| 51 |
],
|
| 52 |
+
outputs=gr.File(label="Download ZIP of Processed Videos"),
|
| 53 |
title="Bulk Video Watermark and Outro Adder",
|
| 54 |
description="Upload multiple videos, a logo image, and an outro video to add watermark and append outro in bulk."
|
| 55 |
)
|