Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
import zipfile
|
| 5 |
+
import shutil
|
| 6 |
+
|
| 7 |
+
def process_videos(video_file_list, logo_file, outro_file):
|
| 8 |
+
if not os.path.exists("outputs"):
|
| 9 |
+
os.makedirs("outputs")
|
| 10 |
+
output_paths = []
|
| 11 |
+
|
| 12 |
+
# Save logo and outro files to disk
|
| 13 |
+
logo_path = "logo.png"
|
| 14 |
+
outro_path = "outro.mp4"
|
| 15 |
+
with open(logo_path, "wb") as f:
|
| 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 {input_path} -i {logo_path} -filter_complex 'overlay=10:10' -codec:a copy {watermarked_path}",
|
| 31 |
+
shell=True
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Add outro
|
| 35 |
+
subprocess.run(
|
| 36 |
+
f"ffmpeg -y -i {watermarked_path} -i {outro_path} -filter_complex '[0:v][1:v]concat=n=2:v=1:a=0[outv]' -map '[outv]' -c:v libx264 {final_path}",
|
| 37 |
+
shell=True
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
output_paths.append(final_path)
|
| 41 |
+
|
| 42 |
+
# Cleanup input and intermediate file
|
| 43 |
+
os.remove(input_path)
|
| 44 |
+
os.remove(watermarked_path)
|
| 45 |
+
|
| 46 |
+
# Create ZIP archive of outputs
|
| 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="file", multiple=True),
|
| 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 ZIP"),
|
| 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 |
+
)
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
demo.launch()
|