Spaces:
Build error
Build error
| import gradio as gr | |
| import subprocess | |
| import os | |
| from tqdm import tqdm | |
| import time | |
| from PIL import Image | |
| from concurrent.futures import ThreadPoolExecutor | |
| import torch | |
| def get_device(): | |
| if torch.cuda.is_available(): | |
| return torch.device('cuda') | |
| else: | |
| return torch.device('cpu') | |
| device = get_device() | |
| print(f"Using device: {device}") | |
| def get_unique_filename(base_name, extension): | |
| counter = 1 | |
| if not base_name.endswith(extension): | |
| unique_name = f"{base_name}{extension}" | |
| else: | |
| unique_name = base_name | |
| while os.path.exists(unique_name): | |
| unique_name = f"{base_name}_{counter}{extension}" | |
| counter += 1 | |
| return unique_name | |
| def get_temp_task_path(input_video_path, task_name): | |
| # Extract the base name of the input video file | |
| base_name = os.path.basename(input_video_path) | |
| print(f"Base name: {base_name}") # Debug print | |
| # Split the base name into words and trim the first 4 words | |
| words = base_name.split() | |
| if len(words) > 4: | |
| trimmed_name = '_'.join(words[4:]) | |
| else: | |
| trimmed_name = base_name # If there are less than 4 words, use the base name as is | |
| print(f"Trimmed name: {trimmed_name}") # Debug print | |
| # Create the new folder name | |
| folder_name = f"{task_name}_{trimmed_name}" | |
| print(f"Folder name: {folder_name}") # Debug print | |
| # Get the directory of the input video file | |
| input_dir = os.path.dirname(input_video_path) | |
| print(f"Input directory: {input_dir}") # Debug print | |
| # Create the full path for the temporary task folder | |
| temp_task_path = os.path.join(input_dir, folder_name) | |
| print(f"Temporary task path: {temp_task_path}") # Debug print | |
| # Create the directory if it doesn't exist | |
| os.makedirs(temp_task_path, exist_ok=True) | |
| print(f"Directory created: {temp_task_path}") # Debug print | |
| return temp_task_path | |
| def combine_videos(video_files, task_name): | |
| if not video_files: | |
| return "No video files selected.", None | |
| # Assuming the first video file is representative for the path | |
| input_dir = os.path.dirname(video_files[0]) | |
| # Use the input directory as the output directory | |
| output_file = os.path.join(input_dir, get_unique_filename("combined_video", ".mp4")) | |
| filelist_path = os.path.join(input_dir, "filelist.txt") | |
| with open(filelist_path, "w") as filelist: | |
| for video in video_files: | |
| video_path = os.path.abspath(video).replace('\\', '/') | |
| filelist.write(f"file '{video_path}'\n") | |
| command = [ | |
| "ffmpeg", "-f", "concat", "-safe", "0", "-i", filelist_path, | |
| "-c", "copy", output_file | |
| ] | |
| with ThreadPoolExecutor() as executor: | |
| future = executor.submit(subprocess.run, command, text=True, capture_output=True) | |
| result = future.result() # Waits for the command to complete and returns the result | |
| if result.returncode == 0: | |
| return f"Videos combined successfully into {output_file}", output_file | |
| else: | |
| return f"Error combining videos: {result.stderr}", None | |
| def combine_audios(audio_files): | |
| if not audio_files: | |
| return "No audio files selected.", None | |
| output_file = get_unique_filename("combined_audio", ".mp3") | |
| filelist_path = os.path.abspath("filelist.txt") | |
| with open(filelist_path, "w") as filelist: | |
| for audio in audio_files: | |
| filelist.write(f"file '{os.path.abspath(audio).replace('\\', '/')}'\n") | |
| command = [ | |
| "ffmpeg", "-f", "concat", "-safe", "0", "-i", filelist_path, | |
| "-c", "copy", output_file | |
| ] | |
| with ThreadPoolExecutor() as executor: | |
| future = executor.submit(subprocess.run, command, text=True, capture_output=True) | |
| result = future.result() # Waits for the command to complete and returns the result | |
| if result.returncode == 0: | |
| return f"Audios combined successfully into {output_file}", output_file | |
| else: | |
| return f"Error combining audios: {result.stderr}", None | |
| def combine_images(image_files): | |
| if not image_files: | |
| return "No image files selected.", None | |
| output_file = get_unique_filename("combined_image", ".mp4") | |
| command = ["convert"] + image_files + [output_file] | |
| process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) | |
| total_time = 0 | |
| with tqdm(total=100, desc="Combining Images") as pbar: | |
| while True: | |
| output = process.stderr.readline() | |
| if output == '' and process.poll() is not None: | |
| break | |
| if output: | |
| total_time += 1 | |
| pbar.update(1) | |
| time.sleep(0.1) | |
| return f"Images combined successfully into {output_file}", output_file | |
| def split_video(input_file, chunk_size): | |
| input_dir = os.path.dirname(input_file) | |
| base_name = os.path.splitext(os.path.basename(input_file))[0] | |
| chunk_files = [] | |
| command = [ | |
| "ffmpeg", "-i", input_file, "-c", "copy", "-map", "0", "-segment_time", str(chunk_size), | |
| "-f", "segment", os.path.join(input_dir, f"{base_name}_chunk_%03d.mp4") | |
| ] | |
| subprocess.run(command, check=True) | |
| for file in os.listdir(input_dir): | |
| if file.startswith(base_name) and file.endswith(".mp4") and "chunk" in file: | |
| chunk_files.append(os.path.join(input_dir, file)) | |
| return chunk_files | |
| def adjust_speed_chunked(media_file, speed, chunk_size=60): | |
| if not media_file: | |
| return "No media file selected.", None | |
| chunk_files = split_video(media_file, chunk_size) | |
| processed_files = [] | |
| def process_chunk(chunk_file): | |
| output_file = get_unique_filename(f"adjusted_speed_{os.path.basename(chunk_file)}", ".mp4") | |
| codec = get_gpu_codec() or "libx264" | |
| command = [ | |
| "ffmpeg", "-hwaccel", "auto", "-i", chunk_file, "-filter:v", f"setpts={1/speed}*PTS", | |
| "-filter:a", f"atempo={speed}", "-c:v", codec, "-preset", "fast", output_file | |
| ] | |
| subprocess.run(command, check=True) | |
| return output_file | |
| with ThreadPoolExecutor() as executor: | |
| futures = [executor.submit(process_chunk, chunk) for chunk in chunk_files] | |
| for future in futures: | |
| processed_files.append(future.result()) | |
| combined_output_file = os.path.join(os.path.dirname(media_file), get_unique_filename("adjusted_speed_combined", ".mp4")) | |
| with open("filelist.txt", "w") as filelist: | |
| for processed_file in processed_files: | |
| filelist.write(f"file '{processed_file}'\n") | |
| command = [ | |
| "ffmpeg", "-f", "concat", "-safe", "0", "-i", "filelist.txt", | |
| "-c", "copy", combined_output_file | |
| ] | |
| subprocess.run(command, check=True) | |
| return f"Speed adjusted successfully to {speed}x in {combined_output_file}", combined_output_file | |
| def adjust_speed(media_file, speed): | |
| if not media_file: | |
| return "No media file selected.", None | |
| output_file = get_unique_filename(f"adjusted_speed_{os.path.basename(media_file)}", ".mp4") | |
| # Determine the codec based on available hardware | |
| codec = get_gpu_codec() | |
| if not codec: | |
| codec = "libx264" # Fallback to a widely supported software codec | |
| command = [ | |
| "ffmpeg", "-hwaccel", "auto", "-i", media_file, "-filter:v", f"setpts={1/speed}*PTS", | |
| "-filter:a", f"atempo={speed}", "-c:v", codec, "-preset", "fast", output_file | |
| ] | |
| process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) | |
| total_time = 0 | |
| with tqdm(total=100, desc="Adjusting Speed") as pbar: | |
| while True: | |
| output = process.stderr.readline() | |
| if output == '' and process.poll() is not None: | |
| break | |
| if output: | |
| total_time += 1 | |
| pbar.update(1) | |
| time.sleep(0.1) | |
| return f"Speed adjusted successfully to {speed}x in {output_file}", output_file | |
| def adjust_speed_by_length(media_file, desired_length_hhmmss): | |
| if not media_file: | |
| return "No media file selected.", None | |
| original_length = get_media_length(media_file) | |
| desired_length = hhmmss_to_seconds(desired_length_hhmmss) | |
| speed = original_length / desired_length | |
| return adjust_speed(media_file, speed) | |
| def adjust_speed_combined(media_file, speed, hours, minutes, seconds, adjust_speed_checkbox, compress_checkbox): | |
| if not adjust_speed_checkbox and not compress_checkbox: | |
| return "Error: At least one of 'Adjust Speed' or 'Compress Video' must be checked.", None | |
| output_file = media_file | |
| output_message = "" | |
| if adjust_speed_checkbox: | |
| if hours or minutes or seconds: | |
| desired_length = f"{int(hours):02}:{int(minutes):02}:{int(seconds):02}" | |
| output_message, output_file = adjust_speed_by_length(media_file, desired_length) | |
| else: | |
| output_message, output_file = adjust_speed_chunked(media_file, speed) | |
| if compress_checkbox and output_file: | |
| compressed_output_file = get_unique_filename(f"compressed_{os.path.basename(output_file)}", ".mp4") | |
| compress_message, output_file = compress_video(output_file, compressed_output_file) | |
| output_message = f"{output_message} and {compress_message}" | |
| return output_message, output_file | |
| def hhmmss_to_seconds(hhmmss): | |
| h, m, s = map(int, hhmmss.split(':')) | |
| return h * 3600 + m * 60 + s | |
| def get_media_length(media_file): | |
| result = subprocess.run( | |
| ["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", media_file], | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.STDOUT | |
| ) | |
| return float(result.stdout) | |
| def auto_color_correct(video_file): | |
| if not video_file: | |
| return "No video file selected.", None | |
| input_dir = os.path.dirname(video_file) | |
| output_file = os.path.join(input_dir, get_unique_filename(f"color_corrected_{os.path.basename(video_file)}", "")) | |
| command = [ | |
| "ffmpeg", "-i", video_file, "-vf", "eq=brightness=0.06:saturation=1.5", output_file | |
| ] | |
| subprocess.run(command, check=True) | |
| return f"Auto color correction applied successfully to {output_file}", output_file | |
| def extract_audio(video_file): | |
| if not video_file: | |
| return "No video file selected.", None | |
| output_file = get_unique_filename(f"extracted_audio_{os.path.basename(video_file)}", ".mka") | |
| command = [ | |
| "ffmpeg", "-i", video_file, "-vn", "-acodec", "copy", output_file | |
| ] | |
| try: | |
| subprocess.run(command, check=True) | |
| except subprocess.CalledProcessError as e: | |
| return f"Error extracting audio: {e}. Command: {' '.join(command)}", None | |
| return f"Audio extracted successfully into {output_file}", output_file | |
| def compress_video(input_file, output_file): | |
| codec = get_gpu_codec() | |
| if not codec: | |
| print("No supported GPU codec found. Falling back to software encoding.") | |
| codec = "libx264" # Fallback to a widely supported software codec | |
| # Adjust these parameters based on your specific needs | |
| bitrate = "1M" # Lower bitrate for smaller file size | |
| maxrate = "1.2M" # Max bitrate to limit peaks in bitrate | |
| bufsize = "2M" # Buffer size for bitrate control | |
| preset = "fast" # Faster preset for quicker compression | |
| command = [ | |
| "ffmpeg", "-hwaccel", "auto", "-i", input_file, | |
| "-c:v", codec, | |
| "-preset", preset, | |
| "-b:v", bitrate, | |
| "-maxrate", maxrate, | |
| "-bufsize", bufsize, | |
| "-profile:v", "high", | |
| "-level", "4.1", | |
| output_file | |
| ] | |
| try: | |
| subprocess.run(command, check=True) | |
| return f"Video compressed successfully into {output_file}", output_file | |
| except subprocess.CalledProcessError as e: | |
| return f"Error compressing video: {e}", None | |
| def add_watermark(video_file, watermark_type, watermark_text, watermark_image, opacity, position_x, position_y, font_size, font_color): | |
| if not video_file: | |
| return "No video file selected.", None | |
| output_file = get_unique_filename(f"watermarked_{os.path.basename(video_file)}", ".mp4") | |
| drawtext = f"drawtext=text='{watermark_text}':x={position_x}:y={position_y}:fontsize={font_size}:fontcolor={font_color}@{opacity}" if watermark_type == "text" else "" | |
| overlay = f"overlay={position_x}:{position_y}" if watermark_type == "image" else "" | |
| command = [ | |
| "ffmpeg", "-i", video_file, | |
| "-vf", f"{drawtext if watermark_type == 'text' else ''}{overlay if watermark_type == 'image' else ''}", | |
| output_file | |
| ] | |
| process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) | |
| total_time = 0 | |
| with tqdm(total=100, desc="Adding Watermark") as pbar: | |
| while True: | |
| output = process.stderr.readline() | |
| if output == '' and process.poll() is not None: | |
| break | |
| if output: | |
| total_time += 1 | |
| pbar.update(1) | |
| time.sleep(0.1) | |
| return f"Watermark added successfully to {output_file}", output_file | |
| def compress_image(input_image): | |
| if not input_image: | |
| return "No image file selected.", None | |
| output_file = get_unique_filename(f"compressed_{os.path.basename(input_image)}", ".jpg") | |
| with Image.open(input_image) as img: | |
| img.save(output_file, "JPEG", quality=95) # Adjust quality as needed | |
| return f"Image compressed successfully into {output_file}", output_file | |
| def compress_image_lossless(input_image): | |
| if not input_image: | |
| return "No image file selected.", None | |
| output_file = get_unique_filename(f"compressed_lossless_{os.path.basename(input_image)}", ".png") | |
| with Image.open(input_image) as img: | |
| img.save(output_file, "PNG", optimize=True) # PNG is lossless | |
| return f"Image compressed losslessly into {output_file}", output_file | |
| def mp3_to_video(mp3_file, image_file): | |
| if not mp3_file or not image_file: | |
| return "MP3 file or image file not selected.", None | |
| output_file = get_unique_filename(f"{os.path.splitext(os.path.basename(mp3_file))[0]}", ".mp4") | |
| command = [ | |
| "ffmpeg", "-loop", "1", "-i", image_file, "-i", mp3_file, "-c:v", "libx264", "-c:a", "aac", "-b:a", "192k", "-shortest", output_file | |
| ] | |
| subprocess.run(command, check=True) | |
| return f"MP3 converted to video successfully into {output_file}", output_file | |
| def video_to_mp3(video_file): | |
| if not video_file: | |
| return "No video file selected.", None | |
| output_file = get_unique_filename(f"{os.path.splitext(os.path.basename(video_file))[0]}", ".mp3") | |
| command = [ | |
| "ffmpeg", "-i", video_file, "-q:a", "0", "-map", "a", output_file | |
| ] | |
| subprocess.run(command, check=True) | |
| return f"Video converted to MP3 successfully into {output_file}", output_file | |
| def convert_image_format(input_image, output_format): | |
| if not input_image: | |
| return "No image file selected.", None | |
| output_file = get_unique_filename(f"{os.path.splitext(os.path.basename(input_image))[0]}", f".{output_format}") | |
| with Image.open(input_image) as img: | |
| img.save(output_file, output_format.upper()) | |
| return f"Image converted to {output_format} format successfully into {output_file}", output_file | |
| def get_gpu_codec(): | |
| # Prioritize Intel QSV codecs for Intel GPUs | |
| codecs = ["h264_qsv", "hevc_qsv", "mpeg2_qsv"] | |
| # Use the -encoders flag to get a list of all available encoders | |
| result = subprocess.run(["ffmpeg", "-hide_banner", "-encoders"], capture_output=True, text=True) | |
| available_encoders = result.stdout | |
| print("Available encoders:", available_encoders) # Debug: print available encoders | |
| for codec in codecs: | |
| # Check if each codec is in the list of available encoders | |
| if codec in available_encoders: | |
| print(f"Using GPU codec: {codec}") # Debug: confirm which codec is used | |
| return codec | |
| print("No supported GPU codec found.") | |
| return None | |
| def interface(): | |
| with gr.Blocks() as demo: | |
| gr.Markdown("### Media Combiner Tool") | |
| with gr.Tab("Combine Videos"): | |
| with gr.Row(): | |
| video_files = gr.File(label="Select Video Files", type="filepath", file_count="multiple") | |
| task_name = gr.Textbox(label="Task Name") | |
| video_submit = gr.Button("Combine Videos") | |
| video_output = gr.Textbox(label="Output") | |
| video_download = gr.File(label="Download Combined Video") | |
| video_submit.click(combine_videos, inputs=[video_files, task_name], outputs=[video_output, video_download]) | |
| with gr.Tab("Combine Audios"): | |
| with gr.Row(): | |
| audio_files = gr.File(label="Select Audio Files", type="filepath", file_count="multiple") | |
| audio_submit = gr.Button("Combine Audios") | |
| audio_output = gr.Textbox(label="Output") | |
| audio_download = gr.File(label="Download Combined Audio") | |
| audio_submit.click(combine_audios, inputs=[audio_files], outputs=[audio_output, audio_download]) | |
| with gr.Tab("Combine Images"): | |
| with gr.Row(): | |
| image_files = gr.File(label="Select Image Files", type="filepath", file_count="multiple") | |
| image_submit = gr.Button("Combine Images") | |
| image_output = gr.Textbox(label="Output") | |
| image_download = gr.File(label="Download Combined Image") | |
| image_submit.click(combine_images, inputs=[image_files], outputs=[image_output, image_download]) | |
| with gr.Tab("Adjust Speed"): | |
| with gr.Row(): | |
| media_file = gr.File(label="Select Media File", type="filepath") | |
| speed = gr.Slider(label="Speed", minimum=0.5, maximum=2.0, step=0.1, value=1.0) | |
| with gr.Row(): | |
| hours = gr.Number(label="Hours", value=0, precision=0) | |
| minutes = gr.Number(label="Minutes", value=0, precision=0) | |
| seconds = gr.Number(label="Seconds", value=0, precision=0) | |
| with gr.Row(): | |
| adjust_speed_checkbox = gr.Checkbox(label="Adjust Speed", value=True) | |
| compress_checkbox = gr.Checkbox(label="Compress Video", value=False) | |
| speed_submit = gr.Button("Submit") | |
| speed_output = gr.Textbox(label="Output") | |
| speed_download = gr.File(label="Download Adjusted Media") | |
| speed_submit.click(adjust_speed_combined, inputs=[media_file, speed, hours, minutes, seconds, adjust_speed_checkbox, compress_checkbox], outputs=[speed_output, speed_download]) | |
| with gr.Tab("Auto Color Correction"): | |
| with gr.Row(): | |
| video_file = gr.File(label="Select Video File", type="filepath") | |
| color_submit = gr.Button("Apply Color Correction") | |
| color_output = gr.Textbox(label="Output") | |
| color_download = gr.File(label="Download Color Corrected Video") | |
| color_submit.click(auto_color_correct, inputs=[video_file], outputs=[color_output, color_download]) | |
| with gr.Tab("Extract Audio"): | |
| with gr.Row(): | |
| video_file = gr.File(label="Select Video File", type="filepath") | |
| extract_submit = gr.Button("Extract Audio") | |
| extract_output = gr.Textbox(label="Output") | |
| extract_download = gr.File(label="Download Extracted Audio") | |
| extract_submit.click(extract_audio, inputs=[video_file], outputs=[extract_output, extract_download]) | |
| with gr.Tab("Add Watermark"): | |
| with gr.Row(): | |
| video_file = gr.File(label="Select Video File", type="filepath") | |
| watermark_type = gr.Radio(label="Watermark Type", choices=["text", "image"], value="text") | |
| with gr.Row(): | |
| watermark_text = gr.Textbox(label="Watermark Text", visible=True) | |
| watermark_image = gr.File(label="Watermark Image", type="filepath", visible=False) | |
| with gr.Row(): | |
| opacity = gr.Slider(label="Opacity", minimum=0.0, maximum=1.0, step=0.1, value=1.0) | |
| position_x = gr.Slider(label="Position X", minimum=0, maximum=1920, step=1, value=0) | |
| position_y = gr.Slider(label="Position Y", minimum=0, maximum=1080, step=1, value=0) | |
| with gr.Row(): | |
| font_size = gr.Slider(label="Font Size", minimum=10, maximum=100, step=1, value=24, visible=True) | |
| font_color = gr.ColorPicker(label="Font Color", value="#FFFFFF", visible=True) | |
| watermark_submit = gr.Button("Add Watermark") | |
| watermark_output = gr.Textbox(label="Output") | |
| watermark_download = gr.File(label="Download Watermarked Video") | |
| def update_visibility(watermark_type): | |
| return { | |
| watermark_text: gr.update(visible=watermark_type == "text"), | |
| watermark_image: gr.update(visible=watermark_type == "image"), | |
| font_size: gr.update(visible=watermark_type == "text"), | |
| font_color: gr.update(visible=watermark_type == "text") | |
| } | |
| watermark_type.change(update_visibility, inputs=[watermark_type], outputs=[watermark_text, watermark_image, font_size, font_color]) | |
| 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]) | |
| with gr.Tab("Compress Image Losslessly"): | |
| with gr.Row(): | |
| image_file = gr.File(label="Select Image File", type="filepath") | |
| compress_submit = gr.Button("Compress Image Losslessly") | |
| compress_output = gr.Textbox(label="Output") | |
| compress_download = gr.File(label="Download Compressed Image") | |
| compress_submit.click(compress_image_lossless, inputs=[image_file], outputs=[compress_output, compress_download]) | |
| with gr.Tab("Convert MP3 to Video"): | |
| with gr.Row(): | |
| mp3_file = gr.File(label="Select MP3 File", type="filepath") | |
| image_file = gr.File(label="Select Image File", type="filepath") | |
| convert_submit = gr.Button("Convert MP3 to Video") | |
| convert_output = gr.Textbox(label="Output") | |
| convert_download = gr.File(label="Download Converted Video") | |
| convert_submit.click(mp3_to_video, inputs=[mp3_file, image_file], outputs=[convert_output, convert_download]) | |
| with gr.Tab("Convert Video to MP3"): | |
| with gr.Row(): | |
| video_file = gr.File(label="Select Video File", type="filepath") | |
| convert_submit = gr.Button("Convert Video to MP3") | |
| convert_output = gr.Textbox(label="Output") | |
| convert_download = gr.File(label="Download Converted MP3") | |
| convert_submit.click(video_to_mp3, inputs=[video_file], outputs=[convert_output, convert_download]) | |
| return demo | |
| if __name__ == "__main__": | |
| demo = interface() | |
| demo.launch(share=True) | |