Python
wip
Gunther Schulz9d23221e30f0
diff --git a/shared/utils/audio_video.py b/shared/utils/audio_video.pyindex 9bf593c..f9210b2 100644--- a/shared/utils/audio_video.py+++ b/shared/utils/audio_video.py@@ -268,9 +268,12 @@ def save_video(tensor,writer.close()# Embed source images if provided and container supports it- if source_images and container == 'mkv':+ if source_images and container in ['mkv', 'mp4']:try:- cache_file = embed_source_images_metadata(cache_file, source_images)+ if container == 'mp4':+ cache_file = embed_source_images_metadata_mp4(cache_file, source_images)+ else: # mkv+ cache_file = embed_source_images_metadata(cache_file, source_images)except Exception as e:print(f"Warning: Failed to embed source images: {e}")@@ -281,9 +284,107 @@ def save_video(tensor,print(f"error saving {save_file}: {e}")+def embed_source_images_metadata_mp4(video_path, source_images):+ """+ Embed source images as cover art in MP4 files using mutagen.++ Args:+ video_path (str): Path to the MP4 video file+ source_images (dict): Dictionary containing source images+ Expected keys: 'image_start', 'image_end', 'image_refs'+ Values should be PIL Images or file paths++ Returns:+ str: Path to the video file with embedded cover art+ """+ from mutagen.mp4 import MP4, MP4Cover, AtomDataType+ import io++ if not source_images:+ return video_path++ try:+ file = MP4(video_path)+ if file.tags is None:+ file.add_tags()++ # Convert source images to cover art+ cover_data = []++ # Process each source image type+ for img_type, img_data in source_images.items():+ if img_data is None:+ continue++ # Handle different image input types+ if isinstance(img_data, list):+ # Multiple images (e.g., image_refs)+ for i, img in enumerate(img_data):+ if img is not None:+ cover_bytes, image_format = _convert_image_to_bytes(img)+ if cover_bytes:+ cover_data.append(MP4Cover(cover_bytes, image_format))+ else:+ # Single image+ cover_bytes, image_format = _convert_image_to_bytes(img_data)+ if cover_bytes:+ cover_data.append(MP4Cover(cover_bytes, image_format))++ if cover_data:+ file.tags['covr'] = cover_data+ file.save()+ print(f"Successfully embedded {len(cover_data)} cover images in {video_path}")++ except Exception as e:+ print(f"Failed to embed cover art with mutagen: {e}")+ print(f"This might be due to image format or MP4 file structure issues")++ return video_path+++def _convert_image_to_bytes(img_data):+ """+ Convert PIL Image or file path to image bytes with proper format detection.++ Args:+ img_data: PIL Image object or file path string++ Returns:+ tuple: (bytes, AtomDataType) - image data and format, or (None, None) if conversion failed+ """+ from mutagen.mp4 import AtomDataType+ import io++ try:+ if hasattr(img_data, 'save'): # PIL Image+ # For PIL Images, save as JPEG for better compatibility+ img_bytes = io.BytesIO()+ img_data.save(img_bytes, format='JPEG')+ return img_bytes.getvalue(), AtomDataType.JPEG++ elif isinstance(img_data, str) and os.path.exists(img_data): # File path+ with open(img_data, 'rb') as f:+ # Detect format based on file extension+ if img_data.lower().endswith(('.jpg', '.jpeg')):+ return f.read(), AtomDataType.JPEG+ elif img_data.lower().endswith('.png'):+ return f.read(), AtomDataType.PNG+ else:+ # Convert unknown formats to JPEG using PIL+ img = Image.open(f)+ img_bytes = io.BytesIO()+ img.save(img_bytes, format='JPEG')+ return img_bytes.getvalue(), AtomDataType.JPEG++ except Exception as e:+ print(f"Failed to convert image to bytes: {e}")+ return None, None++def embed_source_images_metadata(video_path, source_images):"""Embed source images as attachments in MKV video files using FFmpeg.+ For MP4 files, use embed_source_images_metadata_mp4 instead.Args:video_path (str): Path to the video file@@ -500,12 +601,91 @@ def _save_temp_image(img_data, temp_dir, name):return None+def _extract_mp4_cover_art(video_path, output_dir):+ """+ Extract cover art from MP4 files using mutagen.++ Args:+ video_path (str): Path to the MP4 file+ output_dir (str): Directory to save extracted images++ Returns:+ list: List of extracted image file paths+ """+ try:+ from mutagen.mp4 import MP4, AtomDataType+ import os++ print(f"DEBUG: Extracting cover art from MP4: {video_path}")++ file = MP4(video_path)++ if file.tags is None:+ print("DEBUG: No tags found in MP4 file")+ return []++ if 'covr' not in file.tags:+ print("DEBUG: No cover art found in MP4 tags")+ print(f"DEBUG: Available tags: {list(file.tags.keys())}")+ return []++ cover_art = file.tags['covr']+ print(f"DEBUG: Found {len(cover_art)} cover art images")+ extracted_files = []++ # Map cover art to semantic names based on order+ # Since MP4 doesn't preserve semantic meaning, we'll use a heuristic:+ # - First image: image_start+ # - Second image: image_end+ # - Additional images: image_refs+ semantic_names = ['image_start', 'image_end'] + [f'image_refs_{i}' for i in range(10)]++ for i, cover in enumerate(cover_art):+ # Determine file extension based on format+ if cover.imageformat == AtomDataType.JPEG:+ ext = '.jpg'+ elif cover.imageformat == AtomDataType.PNG:+ ext = '.png'+ else:+ ext = '.jpg' # Default to JPEG++ # Create semantic filename for GUI recognition+ if i < len(semantic_names):+ filename = f"{semantic_names[i]}{ext}"+ else:+ filename = f"cover_art_{i+1}{ext}"++ output_file = os.path.join(output_dir, filename)++ print(f"DEBUG: Writing cover art {i+1} to: {filename} ({len(cover):,} bytes)")++ # Write cover art to file+ with open(output_file, 'wb') as f:+ f.write(cover)++ if os.path.exists(output_file):+ extracted_files.append(output_file)+ print(f"DEBUG: Successfully extracted: {filename}")+ else:+ print(f"DEBUG: Failed to create file: {filename}")++ print(f"DEBUG: Total extracted files: {len(extracted_files)}")+ return extracted_files++ except Exception as e:+ print(f"DEBUG: Error extracting cover art from MP4: {e}")+ import traceback+ traceback.print_exc()+ return []++def extract_source_images(video_path, output_dir=None):"""- Extract embedded source images from MKV video files.+ Extract embedded source images from video files.+ Supports MKV (attachments via ffmpeg) and MP4 (cover art via mutagen).Args:- video_path (str): Path to the MKV video file+ video_path (str): Path to the video file (MKV or MP4)output_dir (str): Directory to save extracted images (optional)Returns:@@ -518,6 +698,12 @@ def extract_source_images(video_path, output_dir=None):os.makedirs(output_dir, exist_ok=True)+ # Handle MP4 files with mutagen+ if video_path.lower().endswith('.mp4'):+ print(f"DEBUG: Detected MP4 file, using mutagen extraction")+ return _extract_mp4_cover_art(video_path, output_dir)++ # Handle MKV files with ffmpeg (existing logic)try:# First, probe the video to find attachment streams (attached pics)probe_cmd = [diff --git a/wgp.py b/wgp.pyindex a36fc73..a597afa 100644--- a/wgp.py+++ b/wgp.py@@ -5454,7 +5454,7 @@ def generate_video(else:# Prepare source images for embedding if enabledsource_images = {}- if server_config.get("embed_source_images", False) and server_config.get("video_container", "mp4") == "mkv":+ if server_config.get("embed_source_images", False) and server_config.get("video_container", "mp4") in ["mkv", "mp4"]:if image_start is not None:source_images["image_start"] = image_startif image_end is not None:@@ -6567,16 +6567,17 @@ def export_settings(state):def extract_and_apply_source_images(file_path, state):"""- Extract embedded source images from MKV files and apply them to the UI state.+ Extract embedded source images from video files and apply them to the UI state.+ Supports MKV (attachments) and MP4 (cover art) files.Args:- file_path (str): Path to the MKV video file+ file_path (str): Path to the video file (MKV or MP4)state (dict): UI state dictionaryReturns:int: Number of source images extracted and applied"""- if not file_path.endswith('.mkv'):+ if not (file_path.endswith('.mkv') or file_path.endswith('.mp4')):return 0try:@@ -6587,9 +6588,12 @@ def extract_and_apply_source_images(file_path, state):# Create temporary directory for extracted imageswith tempfile.TemporaryDirectory() as temp_dir:+ print(f"DEBUG GUI: Extracting images from: {file_path}")extracted_files = extract_source_images(file_path, temp_dir)+ print(f"DEBUG GUI: Extracted {len(extracted_files)} files: {[os.path.basename(f) for f in extracted_files]}")if not extracted_files:+ print("DEBUG GUI: No files extracted, returning 0")return 0# Process extracted images and apply to state@@ -6597,13 +6601,16 @@ def extract_and_apply_source_images(file_path, state):for img_path in extracted_files:img_name = os.path.basename(img_path).lower()+ print(f"DEBUG GUI: Processing image: {img_name}")try:# Load the imagepil_image = Image.open(img_path)+ print(f"DEBUG GUI: Loaded image {img_name}: {pil_image.size}")# Apply based on filenameif 'image_start' in img_name:+ print(f"DEBUG GUI: Applying as image_start")# Apply as start imagecurrent_settings = get_model_settings(state, state["model_type"]) or {}current_settings['image_start'] = [pil_image]@@ -6611,6 +6618,7 @@ def extract_and_apply_source_images(file_path, state):applied_count += 1elif 'image_end' in img_name:+ print(f"DEBUG GUI: Applying as image_end")# Apply as end imagecurrent_settings = get_model_settings(state, state["model_type"]) or {}current_settings['image_end'] = [pil_image]@@ -6618,6 +6626,7 @@ def extract_and_apply_source_images(file_path, state):applied_count += 1elif 'image_refs' in img_name:+ print(f"DEBUG GUI: Applying as image_refs")# Apply as reference imagecurrent_settings = get_model_settings(state, state["model_type"]) or {}existing_refs = current_settings.get('image_refs', [])@@ -6658,7 +6667,7 @@ def use_video_settings(state, input_file_list, choice):defaults.update(configs)prompt = configs.get("prompt", "")- # Extract and apply embedded source images from MKV files+ # Extract and apply embedded source images from video filesextracted_images = extract_and_apply_source_images(file_name, state)set_model_settings(state, model_type, defaults)@@ -6804,9 +6813,9 @@ def load_settings_from_file(state, file_path):prompt = configs.get("prompt", "")is_image = configs.get("is_image", False)- # Extract and apply embedded source images from MKV files+ # Extract and apply embedded source images from video filesextracted_images = 0- if file_path.endswith('.mkv'):+ if file_path.endswith('.mkv') or file_path.endswith('.mp4'):extracted_images = extract_and_apply_source_images(file_path, state)if any_video_or_image_file:@@ -9203,8 +9212,8 @@ def generate_configuration_tab(state, blocks, header, model_family, model_choiceembed_source_images_choice = gr.Checkbox(value=server_config.get("embed_source_images", False),- label="Embed Source Images in Video Files (requires MKV format)",- info="Automatically embeds i2v source images as attachments in the video file for reference"+ label="Embed Source Images in Video Files",+ info="Automatically embeds i2v source images in the video file for reference (MP4: cover art, MKV: attachments)")image_output_codec_choice = gr.Dropdown(