Spaces:
Build error
Build error
Upload 5 files
Browse files- .gitattributes +35 -35
- README.md +13 -13
- Video_combiner_updated-.py +525 -0
- app.py +1 -0
- requirements.txt +1 -0
.gitattributes
CHANGED
|
@@ -1,35 +1,35 @@
|
|
| 1 |
-
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: Media Utilities
|
| 3 |
-
emoji: 🔥
|
| 4 |
-
colorFrom: red
|
| 5 |
-
colorTo: pink
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 4.36.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
license: mit
|
| 11 |
-
---
|
| 12 |
-
|
| 13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Media Utilities
|
| 3 |
+
emoji: 🔥
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: pink
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.36.0
|
| 8 |
+
app_file: app.py
|
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
from tqdm import tqdm
|
| 5 |
+
import time
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 8 |
+
import torch
|
| 9 |
+
|
| 10 |
+
def get_device():
|
| 11 |
+
if torch.cuda.is_available():
|
| 12 |
+
return torch.device('cuda')
|
| 13 |
+
else:
|
| 14 |
+
return torch.device('cpu')
|
| 15 |
+
|
| 16 |
+
device = get_device()
|
| 17 |
+
print(f"Using device: {device}")
|
| 18 |
+
|
| 19 |
+
def get_unique_filename(base_name, extension):
|
| 20 |
+
counter = 1
|
| 21 |
+
if not base_name.endswith(extension):
|
| 22 |
+
unique_name = f"{base_name}{extension}"
|
| 23 |
+
else:
|
| 24 |
+
unique_name = base_name
|
| 25 |
+
while os.path.exists(unique_name):
|
| 26 |
+
unique_name = f"{base_name}_{counter}{extension}"
|
| 27 |
+
counter += 1
|
| 28 |
+
return unique_name
|
| 29 |
+
|
| 30 |
+
def get_temp_task_path(input_video_path, task_name):
|
| 31 |
+
# Extract the base name of the input video file
|
| 32 |
+
base_name = os.path.basename(input_video_path)
|
| 33 |
+
print(f"Base name: {base_name}") # Debug print
|
| 34 |
+
|
| 35 |
+
# Split the base name into words and trim the first 4 words
|
| 36 |
+
words = base_name.split()
|
| 37 |
+
if len(words) > 4:
|
| 38 |
+
trimmed_name = '_'.join(words[4:])
|
| 39 |
+
else:
|
| 40 |
+
trimmed_name = base_name # If there are less than 4 words, use the base name as is
|
| 41 |
+
print(f"Trimmed name: {trimmed_name}") # Debug print
|
| 42 |
+
|
| 43 |
+
# Create the new folder name
|
| 44 |
+
folder_name = f"{task_name}_{trimmed_name}"
|
| 45 |
+
print(f"Folder name: {folder_name}") # Debug print
|
| 46 |
+
|
| 47 |
+
# Get the directory of the input video file
|
| 48 |
+
input_dir = os.path.dirname(input_video_path)
|
| 49 |
+
print(f"Input directory: {input_dir}") # Debug print
|
| 50 |
+
|
| 51 |
+
# Create the full path for the temporary task folder
|
| 52 |
+
temp_task_path = os.path.join(input_dir, folder_name)
|
| 53 |
+
print(f"Temporary task path: {temp_task_path}") # Debug print
|
| 54 |
+
|
| 55 |
+
# Create the directory if it doesn't exist
|
| 56 |
+
os.makedirs(temp_task_path, exist_ok=True)
|
| 57 |
+
print(f"Directory created: {temp_task_path}") # Debug print
|
| 58 |
+
|
| 59 |
+
return temp_task_path
|
| 60 |
+
|
| 61 |
+
def combine_videos(video_files, task_name):
|
| 62 |
+
if not video_files:
|
| 63 |
+
return "No video files selected.", None
|
| 64 |
+
|
| 65 |
+
# Assuming the first video file is representative for the path
|
| 66 |
+
input_dir = os.path.dirname(video_files[0])
|
| 67 |
+
|
| 68 |
+
# Use the input directory as the output directory
|
| 69 |
+
output_file = os.path.join(input_dir, get_unique_filename("combined_video", ".mp4"))
|
| 70 |
+
filelist_path = os.path.join(input_dir, "filelist.txt")
|
| 71 |
+
|
| 72 |
+
with open(filelist_path, "w") as filelist:
|
| 73 |
+
for video in video_files:
|
| 74 |
+
video_path = os.path.abspath(video).replace('\\', '/')
|
| 75 |
+
filelist.write(f"file '{video_path}'\n")
|
| 76 |
+
|
| 77 |
+
command = [
|
| 78 |
+
"ffmpeg", "-f", "concat", "-safe", "0", "-i", filelist_path,
|
| 79 |
+
"-c", "copy", output_file
|
| 80 |
+
]
|
| 81 |
+
|
| 82 |
+
with ThreadPoolExecutor() as executor:
|
| 83 |
+
future = executor.submit(subprocess.run, command, text=True, capture_output=True)
|
| 84 |
+
result = future.result() # Waits for the command to complete and returns the result
|
| 85 |
+
|
| 86 |
+
if result.returncode == 0:
|
| 87 |
+
return f"Videos combined successfully into {output_file}", output_file
|
| 88 |
+
else:
|
| 89 |
+
return f"Error combining videos: {result.stderr}", None
|
| 90 |
+
|
| 91 |
+
def combine_audios(audio_files):
|
| 92 |
+
if not audio_files:
|
| 93 |
+
return "No audio files selected.", None
|
| 94 |
+
output_file = get_unique_filename("combined_audio", ".mp3")
|
| 95 |
+
filelist_path = os.path.abspath("filelist.txt")
|
| 96 |
+
with open(filelist_path, "w") as filelist:
|
| 97 |
+
for audio in audio_files:
|
| 98 |
+
filelist.write(f"file '{os.path.abspath(audio).replace('\\', '/')}'\n")
|
| 99 |
+
|
| 100 |
+
command = [
|
| 101 |
+
"ffmpeg", "-f", "concat", "-safe", "0", "-i", filelist_path,
|
| 102 |
+
"-c", "copy", output_file
|
| 103 |
+
]
|
| 104 |
+
|
| 105 |
+
with ThreadPoolExecutor() as executor:
|
| 106 |
+
future = executor.submit(subprocess.run, command, text=True, capture_output=True)
|
| 107 |
+
result = future.result() # Waits for the command to complete and returns the result
|
| 108 |
+
|
| 109 |
+
if result.returncode == 0:
|
| 110 |
+
return f"Audios combined successfully into {output_file}", output_file
|
| 111 |
+
else:
|
| 112 |
+
return f"Error combining audios: {result.stderr}", None
|
| 113 |
+
|
| 114 |
+
def combine_images(image_files):
|
| 115 |
+
if not image_files:
|
| 116 |
+
return "No image files selected.", None
|
| 117 |
+
output_file = get_unique_filename("combined_image", ".mp4")
|
| 118 |
+
command = ["convert"] + image_files + [output_file]
|
| 119 |
+
|
| 120 |
+
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 121 |
+
total_time = 0
|
| 122 |
+
with tqdm(total=100, desc="Combining Images") as pbar:
|
| 123 |
+
while True:
|
| 124 |
+
output = process.stderr.readline()
|
| 125 |
+
if output == '' and process.poll() is not None:
|
| 126 |
+
break
|
| 127 |
+
if output:
|
| 128 |
+
total_time += 1
|
| 129 |
+
pbar.update(1)
|
| 130 |
+
time.sleep(0.1)
|
| 131 |
+
|
| 132 |
+
return f"Images combined successfully into {output_file}", output_file
|
| 133 |
+
|
| 134 |
+
def split_video(input_file, chunk_size):
|
| 135 |
+
input_dir = os.path.dirname(input_file)
|
| 136 |
+
base_name = os.path.splitext(os.path.basename(input_file))[0]
|
| 137 |
+
chunk_files = []
|
| 138 |
+
|
| 139 |
+
command = [
|
| 140 |
+
"ffmpeg", "-i", input_file, "-c", "copy", "-map", "0", "-segment_time", str(chunk_size),
|
| 141 |
+
"-f", "segment", os.path.join(input_dir, f"{base_name}_chunk_%03d.mp4")
|
| 142 |
+
]
|
| 143 |
+
subprocess.run(command, check=True)
|
| 144 |
+
|
| 145 |
+
for file in os.listdir(input_dir):
|
| 146 |
+
if file.startswith(base_name) and file.endswith(".mp4") and "chunk" in file:
|
| 147 |
+
chunk_files.append(os.path.join(input_dir, file))
|
| 148 |
+
|
| 149 |
+
return chunk_files
|
| 150 |
+
|
| 151 |
+
def adjust_speed_chunked(media_file, speed, chunk_size=60):
|
| 152 |
+
if not media_file:
|
| 153 |
+
return "No media file selected.", None
|
| 154 |
+
|
| 155 |
+
chunk_files = split_video(media_file, chunk_size)
|
| 156 |
+
processed_files = []
|
| 157 |
+
|
| 158 |
+
def process_chunk(chunk_file):
|
| 159 |
+
output_file = get_unique_filename(f"adjusted_speed_{os.path.basename(chunk_file)}", ".mp4")
|
| 160 |
+
codec = get_gpu_codec() or "libx264"
|
| 161 |
+
command = [
|
| 162 |
+
"ffmpeg", "-hwaccel", "auto", "-i", chunk_file, "-filter:v", f"setpts={1/speed}*PTS",
|
| 163 |
+
"-filter:a", f"atempo={speed}", "-c:v", codec, "-preset", "fast", output_file
|
| 164 |
+
]
|
| 165 |
+
subprocess.run(command, check=True)
|
| 166 |
+
return output_file
|
| 167 |
+
|
| 168 |
+
with ThreadPoolExecutor() as executor:
|
| 169 |
+
futures = [executor.submit(process_chunk, chunk) for chunk in chunk_files]
|
| 170 |
+
for future in futures:
|
| 171 |
+
processed_files.append(future.result())
|
| 172 |
+
|
| 173 |
+
combined_output_file = os.path.join(os.path.dirname(media_file), get_unique_filename("adjusted_speed_combined", ".mp4"))
|
| 174 |
+
with open("filelist.txt", "w") as filelist:
|
| 175 |
+
for processed_file in processed_files:
|
| 176 |
+
filelist.write(f"file '{processed_file}'\n")
|
| 177 |
+
|
| 178 |
+
command = [
|
| 179 |
+
"ffmpeg", "-f", "concat", "-safe", "0", "-i", "filelist.txt",
|
| 180 |
+
"-c", "copy", combined_output_file
|
| 181 |
+
]
|
| 182 |
+
subprocess.run(command, check=True)
|
| 183 |
+
|
| 184 |
+
return f"Speed adjusted successfully to {speed}x in {combined_output_file}", combined_output_file
|
| 185 |
+
|
| 186 |
+
def adjust_speed(media_file, speed):
|
| 187 |
+
if not media_file:
|
| 188 |
+
return "No media file selected.", None
|
| 189 |
+
output_file = get_unique_filename(f"adjusted_speed_{os.path.basename(media_file)}", ".mp4")
|
| 190 |
+
|
| 191 |
+
# Determine the codec based on available hardware
|
| 192 |
+
codec = get_gpu_codec()
|
| 193 |
+
if not codec:
|
| 194 |
+
codec = "libx264" # Fallback to a widely supported software codec
|
| 195 |
+
|
| 196 |
+
command = [
|
| 197 |
+
"ffmpeg", "-hwaccel", "auto", "-i", media_file, "-filter:v", f"setpts={1/speed}*PTS",
|
| 198 |
+
"-filter:a", f"atempo={speed}", "-c:v", codec, "-preset", "fast", output_file
|
| 199 |
+
]
|
| 200 |
+
|
| 201 |
+
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 202 |
+
total_time = 0
|
| 203 |
+
with tqdm(total=100, desc="Adjusting Speed") as pbar:
|
| 204 |
+
while True:
|
| 205 |
+
output = process.stderr.readline()
|
| 206 |
+
if output == '' and process.poll() is not None:
|
| 207 |
+
break
|
| 208 |
+
if output:
|
| 209 |
+
total_time += 1
|
| 210 |
+
pbar.update(1)
|
| 211 |
+
time.sleep(0.1)
|
| 212 |
+
|
| 213 |
+
return f"Speed adjusted successfully to {speed}x in {output_file}", output_file
|
| 214 |
+
|
| 215 |
+
def adjust_speed_by_length(media_file, desired_length_hhmmss):
|
| 216 |
+
if not media_file:
|
| 217 |
+
return "No media file selected.", None
|
| 218 |
+
original_length = get_media_length(media_file)
|
| 219 |
+
desired_length = hhmmss_to_seconds(desired_length_hhmmss)
|
| 220 |
+
speed = original_length / desired_length
|
| 221 |
+
return adjust_speed(media_file, speed)
|
| 222 |
+
|
| 223 |
+
def adjust_speed_combined(media_file, speed, hours, minutes, seconds, adjust_speed_checkbox, compress_checkbox):
|
| 224 |
+
if not adjust_speed_checkbox and not compress_checkbox:
|
| 225 |
+
return "Error: At least one of 'Adjust Speed' or 'Compress Video' must be checked.", None
|
| 226 |
+
|
| 227 |
+
output_file = media_file
|
| 228 |
+
output_message = ""
|
| 229 |
+
|
| 230 |
+
if adjust_speed_checkbox:
|
| 231 |
+
if hours or minutes or seconds:
|
| 232 |
+
desired_length = f"{int(hours):02}:{int(minutes):02}:{int(seconds):02}"
|
| 233 |
+
output_message, output_file = adjust_speed_by_length(media_file, desired_length)
|
| 234 |
+
else:
|
| 235 |
+
output_message, output_file = adjust_speed_chunked(media_file, speed)
|
| 236 |
+
|
| 237 |
+
if compress_checkbox and output_file:
|
| 238 |
+
compressed_output_file = get_unique_filename(f"compressed_{os.path.basename(output_file)}", ".mp4")
|
| 239 |
+
compress_message, output_file = compress_video(output_file, compressed_output_file)
|
| 240 |
+
output_message = f"{output_message} and {compress_message}"
|
| 241 |
+
|
| 242 |
+
return output_message, output_file
|
| 243 |
+
|
| 244 |
+
def hhmmss_to_seconds(hhmmss):
|
| 245 |
+
h, m, s = map(int, hhmmss.split(':'))
|
| 246 |
+
return h * 3600 + m * 60 + s
|
| 247 |
+
|
| 248 |
+
def get_media_length(media_file):
|
| 249 |
+
result = subprocess.run(
|
| 250 |
+
["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", media_file],
|
| 251 |
+
stdout=subprocess.PIPE,
|
| 252 |
+
stderr=subprocess.STDOUT
|
| 253 |
+
)
|
| 254 |
+
return float(result.stdout)
|
| 255 |
+
|
| 256 |
+
def auto_color_correct(video_file):
|
| 257 |
+
if not video_file:
|
| 258 |
+
return "No video file selected.", None
|
| 259 |
+
|
| 260 |
+
input_dir = os.path.dirname(video_file)
|
| 261 |
+
output_file = os.path.join(input_dir, get_unique_filename(f"color_corrected_{os.path.basename(video_file)}", ""))
|
| 262 |
+
|
| 263 |
+
command = [
|
| 264 |
+
"ffmpeg", "-i", video_file, "-vf", "eq=brightness=0.06:saturation=1.5", output_file
|
| 265 |
+
]
|
| 266 |
+
subprocess.run(command, check=True)
|
| 267 |
+
return f"Auto color correction applied successfully to {output_file}", output_file
|
| 268 |
+
|
| 269 |
+
def extract_audio(video_file):
|
| 270 |
+
if not video_file:
|
| 271 |
+
return "No video file selected.", None
|
| 272 |
+
output_file = get_unique_filename(f"extracted_audio_{os.path.basename(video_file)}", ".mka")
|
| 273 |
+
command = [
|
| 274 |
+
"ffmpeg", "-i", video_file, "-vn", "-acodec", "copy", output_file
|
| 275 |
+
]
|
| 276 |
+
try:
|
| 277 |
+
subprocess.run(command, check=True)
|
| 278 |
+
except subprocess.CalledProcessError as e:
|
| 279 |
+
return f"Error extracting audio: {e}. Command: {' '.join(command)}", None
|
| 280 |
+
return f"Audio extracted successfully into {output_file}", output_file
|
| 281 |
+
|
| 282 |
+
def compress_video(input_file, output_file):
|
| 283 |
+
codec = get_gpu_codec()
|
| 284 |
+
if not codec:
|
| 285 |
+
print("No supported GPU codec found. Falling back to software encoding.")
|
| 286 |
+
codec = "libx264" # Fallback to a widely supported software codec
|
| 287 |
+
|
| 288 |
+
# Adjust these parameters based on your specific needs
|
| 289 |
+
bitrate = "1M" # Lower bitrate for smaller file size
|
| 290 |
+
maxrate = "1.2M" # Max bitrate to limit peaks in bitrate
|
| 291 |
+
bufsize = "2M" # Buffer size for bitrate control
|
| 292 |
+
preset = "fast" # Faster preset for quicker compression
|
| 293 |
+
|
| 294 |
+
command = [
|
| 295 |
+
"ffmpeg", "-hwaccel", "auto", "-i", input_file,
|
| 296 |
+
"-c:v", codec,
|
| 297 |
+
"-preset", preset,
|
| 298 |
+
"-b:v", bitrate,
|
| 299 |
+
"-maxrate", maxrate,
|
| 300 |
+
"-bufsize", bufsize,
|
| 301 |
+
"-profile:v", "high",
|
| 302 |
+
"-level", "4.1",
|
| 303 |
+
output_file
|
| 304 |
+
]
|
| 305 |
+
try:
|
| 306 |
+
subprocess.run(command, check=True)
|
| 307 |
+
return f"Video compressed successfully into {output_file}", output_file
|
| 308 |
+
except subprocess.CalledProcessError as e:
|
| 309 |
+
return f"Error compressing video: {e}", None
|
| 310 |
+
|
| 311 |
+
def add_watermark(video_file, watermark_type, watermark_text, watermark_image, opacity, position_x, position_y, font_size, font_color):
|
| 312 |
+
if not video_file:
|
| 313 |
+
return "No video file selected.", None
|
| 314 |
+
|
| 315 |
+
output_file = get_unique_filename(f"watermarked_{os.path.basename(video_file)}", ".mp4")
|
| 316 |
+
drawtext = f"drawtext=text='{watermark_text}':x={position_x}:y={position_y}:fontsize={font_size}:fontcolor={font_color}@{opacity}" if watermark_type == "text" else ""
|
| 317 |
+
overlay = f"overlay={position_x}:{position_y}" if watermark_type == "image" else ""
|
| 318 |
+
|
| 319 |
+
command = [
|
| 320 |
+
"ffmpeg", "-i", video_file,
|
| 321 |
+
"-vf", f"{drawtext if watermark_type == 'text' else ''}{overlay if watermark_type == 'image' else ''}",
|
| 322 |
+
output_file
|
| 323 |
+
]
|
| 324 |
+
|
| 325 |
+
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 326 |
+
total_time = 0
|
| 327 |
+
with tqdm(total=100, desc="Adding Watermark") as pbar:
|
| 328 |
+
while True:
|
| 329 |
+
output = process.stderr.readline()
|
| 330 |
+
if output == '' and process.poll() is not None:
|
| 331 |
+
break
|
| 332 |
+
if output:
|
| 333 |
+
total_time += 1
|
| 334 |
+
pbar.update(1)
|
| 335 |
+
time.sleep(0.1)
|
| 336 |
+
|
| 337 |
+
return f"Watermark added successfully to {output_file}", output_file
|
| 338 |
+
|
| 339 |
+
def compress_image(input_image):
|
| 340 |
+
if not input_image:
|
| 341 |
+
return "No image file selected.", None
|
| 342 |
+
output_file = get_unique_filename(f"compressed_{os.path.basename(input_image)}", ".jpg")
|
| 343 |
+
|
| 344 |
+
with Image.open(input_image) as img:
|
| 345 |
+
img.save(output_file, "JPEG", quality=95) # Adjust quality as needed
|
| 346 |
+
|
| 347 |
+
return f"Image compressed successfully into {output_file}", output_file
|
| 348 |
+
|
| 349 |
+
def compress_image_lossless(input_image):
|
| 350 |
+
if not input_image:
|
| 351 |
+
return "No image file selected.", None
|
| 352 |
+
output_file = get_unique_filename(f"compressed_lossless_{os.path.basename(input_image)}", ".png")
|
| 353 |
+
|
| 354 |
+
with Image.open(input_image) as img:
|
| 355 |
+
img.save(output_file, "PNG", optimize=True) # PNG is lossless
|
| 356 |
+
|
| 357 |
+
return f"Image compressed losslessly into {output_file}", output_file
|
| 358 |
+
|
| 359 |
+
def mp3_to_video(mp3_file, image_file):
|
| 360 |
+
if not mp3_file or not image_file:
|
| 361 |
+
return "MP3 file or image file not selected.", None
|
| 362 |
+
output_file = get_unique_filename(f"{os.path.splitext(os.path.basename(mp3_file))[0]}", ".mp4")
|
| 363 |
+
command = [
|
| 364 |
+
"ffmpeg", "-loop", "1", "-i", image_file, "-i", mp3_file, "-c:v", "libx264", "-c:a", "aac", "-b:a", "192k", "-shortest", output_file
|
| 365 |
+
]
|
| 366 |
+
subprocess.run(command, check=True)
|
| 367 |
+
return f"MP3 converted to video successfully into {output_file}", output_file
|
| 368 |
+
|
| 369 |
+
def video_to_mp3(video_file):
|
| 370 |
+
if not video_file:
|
| 371 |
+
return "No video file selected.", None
|
| 372 |
+
output_file = get_unique_filename(f"{os.path.splitext(os.path.basename(video_file))[0]}", ".mp3")
|
| 373 |
+
command = [
|
| 374 |
+
"ffmpeg", "-i", video_file, "-q:a", "0", "-map", "a", output_file
|
| 375 |
+
]
|
| 376 |
+
subprocess.run(command, check=True)
|
| 377 |
+
return f"Video converted to MP3 successfully into {output_file}", output_file
|
| 378 |
+
|
| 379 |
+
def convert_image_format(input_image, output_format):
|
| 380 |
+
if not input_image:
|
| 381 |
+
return "No image file selected.", None
|
| 382 |
+
output_file = get_unique_filename(f"{os.path.splitext(os.path.basename(input_image))[0]}", f".{output_format}")
|
| 383 |
+
|
| 384 |
+
with Image.open(input_image) as img:
|
| 385 |
+
img.save(output_file, output_format.upper())
|
| 386 |
+
|
| 387 |
+
return f"Image converted to {output_format} format successfully into {output_file}", output_file
|
| 388 |
+
|
| 389 |
+
|
| 390 |
+
def get_gpu_codec():
|
| 391 |
+
# Prioritize Intel QSV codecs for Intel GPUs
|
| 392 |
+
codecs = ["h264_qsv", "hevc_qsv", "mpeg2_qsv"]
|
| 393 |
+
# Use the -encoders flag to get a list of all available encoders
|
| 394 |
+
result = subprocess.run(["ffmpeg", "-hide_banner", "-encoders"], capture_output=True, text=True)
|
| 395 |
+
available_encoders = result.stdout
|
| 396 |
+
print("Available encoders:", available_encoders) # Debug: print available encoders
|
| 397 |
+
for codec in codecs:
|
| 398 |
+
# Check if each codec is in the list of available encoders
|
| 399 |
+
if codec in available_encoders:
|
| 400 |
+
print(f"Using GPU codec: {codec}") # Debug: confirm which codec is used
|
| 401 |
+
return codec
|
| 402 |
+
print("No supported GPU codec found.")
|
| 403 |
+
return None
|
| 404 |
+
|
| 405 |
+
def interface():
|
| 406 |
+
with gr.Blocks() as demo:
|
| 407 |
+
gr.Markdown("### Media Combiner Tool")
|
| 408 |
+
|
| 409 |
+
with gr.Tab("Combine Videos"):
|
| 410 |
+
with gr.Row():
|
| 411 |
+
video_files = gr.File(label="Select Video Files", type="filepath", file_count="multiple")
|
| 412 |
+
task_name = gr.Textbox(label="Task Name")
|
| 413 |
+
video_submit = gr.Button("Combine Videos")
|
| 414 |
+
video_output = gr.Textbox(label="Output")
|
| 415 |
+
video_download = gr.File(label="Download Combined Video")
|
| 416 |
+
video_submit.click(combine_videos, inputs=[video_files, task_name], outputs=[video_output, video_download])
|
| 417 |
+
|
| 418 |
+
with gr.Tab("Combine Audios"):
|
| 419 |
+
with gr.Row():
|
| 420 |
+
audio_files = gr.File(label="Select Audio Files", type="filepath", file_count="multiple")
|
| 421 |
+
audio_submit = gr.Button("Combine Audios")
|
| 422 |
+
audio_output = gr.Textbox(label="Output")
|
| 423 |
+
audio_download = gr.File(label="Download Combined Audio")
|
| 424 |
+
audio_submit.click(combine_audios, inputs=[audio_files], outputs=[audio_output, audio_download])
|
| 425 |
+
|
| 426 |
+
with gr.Tab("Combine Images"):
|
| 427 |
+
with gr.Row():
|
| 428 |
+
image_files = gr.File(label="Select Image Files", type="filepath", file_count="multiple")
|
| 429 |
+
image_submit = gr.Button("Combine Images")
|
| 430 |
+
image_output = gr.Textbox(label="Output")
|
| 431 |
+
image_download = gr.File(label="Download Combined Image")
|
| 432 |
+
image_submit.click(combine_images, inputs=[image_files], outputs=[image_output, image_download])
|
| 433 |
+
|
| 434 |
+
with gr.Tab("Adjust Speed"):
|
| 435 |
+
with gr.Row():
|
| 436 |
+
media_file = gr.File(label="Select Media File", type="filepath")
|
| 437 |
+
speed = gr.Slider(label="Speed", minimum=0.5, maximum=2.0, step=0.1, value=1.0)
|
| 438 |
+
with gr.Row():
|
| 439 |
+
hours = gr.Number(label="Hours", value=0, precision=0)
|
| 440 |
+
minutes = gr.Number(label="Minutes", value=0, precision=0)
|
| 441 |
+
seconds = gr.Number(label="Seconds", value=0, precision=0)
|
| 442 |
+
with gr.Row():
|
| 443 |
+
adjust_speed_checkbox = gr.Checkbox(label="Adjust Speed", value=True)
|
| 444 |
+
compress_checkbox = gr.Checkbox(label="Compress Video", value=False)
|
| 445 |
+
speed_submit = gr.Button("Submit")
|
| 446 |
+
speed_output = gr.Textbox(label="Output")
|
| 447 |
+
speed_download = gr.File(label="Download Adjusted Media")
|
| 448 |
+
speed_submit.click(adjust_speed_combined, inputs=[media_file, speed, hours, minutes, seconds, adjust_speed_checkbox, compress_checkbox], outputs=[speed_output, speed_download])
|
| 449 |
+
|
| 450 |
+
with gr.Tab("Auto Color Correction"):
|
| 451 |
+
with gr.Row():
|
| 452 |
+
video_file = gr.File(label="Select Video File", type="filepath")
|
| 453 |
+
color_submit = gr.Button("Apply Color Correction")
|
| 454 |
+
color_output = gr.Textbox(label="Output")
|
| 455 |
+
color_download = gr.File(label="Download Color Corrected Video")
|
| 456 |
+
color_submit.click(auto_color_correct, inputs=[video_file], outputs=[color_output, color_download])
|
| 457 |
+
|
| 458 |
+
with gr.Tab("Extract Audio"):
|
| 459 |
+
with gr.Row():
|
| 460 |
+
video_file = gr.File(label="Select Video File", type="filepath")
|
| 461 |
+
extract_submit = gr.Button("Extract Audio")
|
| 462 |
+
extract_output = gr.Textbox(label="Output")
|
| 463 |
+
extract_download = gr.File(label="Download Extracted Audio")
|
| 464 |
+
extract_submit.click(extract_audio, inputs=[video_file], outputs=[extract_output, extract_download])
|
| 465 |
+
|
| 466 |
+
with gr.Tab("Add Watermark"):
|
| 467 |
+
with gr.Row():
|
| 468 |
+
video_file = gr.File(label="Select Video File", type="filepath")
|
| 469 |
+
watermark_type = gr.Radio(label="Watermark Type", choices=["text", "image"], value="text")
|
| 470 |
+
with gr.Row():
|
| 471 |
+
watermark_text = gr.Textbox(label="Watermark Text", visible=True)
|
| 472 |
+
watermark_image = gr.File(label="Watermark Image", type="filepath", visible=False)
|
| 473 |
+
with gr.Row():
|
| 474 |
+
opacity = gr.Slider(label="Opacity", minimum=0.0, maximum=1.0, step=0.1, value=1.0)
|
| 475 |
+
position_x = gr.Slider(label="Position X", minimum=0, maximum=1920, step=1, value=0)
|
| 476 |
+
position_y = gr.Slider(label="Position Y", minimum=0, maximum=1080, step=1, value=0)
|
| 477 |
+
with gr.Row():
|
| 478 |
+
font_size = gr.Slider(label="Font Size", minimum=10, maximum=100, step=1, value=24, visible=True)
|
| 479 |
+
font_color = gr.ColorPicker(label="Font Color", value="#FFFFFF", visible=True)
|
| 480 |
+
watermark_submit = gr.Button("Add Watermark")
|
| 481 |
+
watermark_output = gr.Textbox(label="Output")
|
| 482 |
+
watermark_download = gr.File(label="Download Watermarked Video")
|
| 483 |
+
|
| 484 |
+
def update_visibility(watermark_type):
|
| 485 |
+
return {
|
| 486 |
+
watermark_text: gr.update(visible=watermark_type == "text"),
|
| 487 |
+
watermark_image: gr.update(visible=watermark_type == "image"),
|
| 488 |
+
font_size: gr.update(visible=watermark_type == "text"),
|
| 489 |
+
font_color: gr.update(visible=watermark_type == "text")
|
| 490 |
+
}
|
| 491 |
+
|
| 492 |
+
watermark_type.change(update_visibility, inputs=[watermark_type], outputs=[watermark_text, watermark_image, font_size, font_color])
|
| 493 |
+
watermark_submit.click(add_watermark, inputs=[video_file, watermark_type, watermark_text, watermark_image, opacity, position_x, position_y, font_size, font_color], outputs=[watermark_output, watermark_download])
|
| 494 |
+
|
| 495 |
+
with gr.Tab("Compress Image Losslessly"):
|
| 496 |
+
with gr.Row():
|
| 497 |
+
image_file = gr.File(label="Select Image File", type="filepath")
|
| 498 |
+
compress_submit = gr.Button("Compress Image Losslessly")
|
| 499 |
+
compress_output = gr.Textbox(label="Output")
|
| 500 |
+
compress_download = gr.File(label="Download Compressed Image")
|
| 501 |
+
compress_submit.click(compress_image_lossless, inputs=[image_file], outputs=[compress_output, compress_download])
|
| 502 |
+
|
| 503 |
+
with gr.Tab("Convert MP3 to Video"):
|
| 504 |
+
with gr.Row():
|
| 505 |
+
mp3_file = gr.File(label="Select MP3 File", type="filepath")
|
| 506 |
+
image_file = gr.File(label="Select Image File", type="filepath")
|
| 507 |
+
convert_submit = gr.Button("Convert MP3 to Video")
|
| 508 |
+
convert_output = gr.Textbox(label="Output")
|
| 509 |
+
convert_download = gr.File(label="Download Converted Video")
|
| 510 |
+
convert_submit.click(mp3_to_video, inputs=[mp3_file, image_file], outputs=[convert_output, convert_download])
|
| 511 |
+
|
| 512 |
+
with gr.Tab("Convert Video to MP3"):
|
| 513 |
+
with gr.Row():
|
| 514 |
+
video_file = gr.File(label="Select Video File", type="filepath")
|
| 515 |
+
convert_submit = gr.Button("Convert Video to MP3")
|
| 516 |
+
convert_output = gr.Textbox(label="Output")
|
| 517 |
+
convert_download = gr.File(label="Download Converted MP3")
|
| 518 |
+
convert_submit.click(video_to_mp3, inputs=[video_file], outputs=[convert_output, convert_download])
|
| 519 |
+
|
| 520 |
+
return demo
|
| 521 |
+
|
| 522 |
+
|
| 523 |
+
if __name__ == "__main__":
|
| 524 |
+
demo = interface()
|
| 525 |
+
demo.launch(share=True)
|
app.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
-e "from Video_combiner_updated import interface\n\ndemo = interface()\n\nif __name__ == '__main__':\n demo.launch()"
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
-e "gradio\ntorch\ntqdm\npillow"
|