Upload Mp4 merge.ipynb
Browse files- Mp4 merge.ipynb +1 -1
Mp4 merge.ipynb
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
{"cells":[{"cell_type":"code","source":["# Install
|
|
|
|
| 1 |
+
{"cells":[{"cell_type":"code","source":["# Install ImageMagick and dependencies\n","!apt-get update\n","!apt-get install -y imagemagick\n","\n","# Modify ImageMagick policy to allow execution\n","!sed -i '/<policy domain=\"path\" rights=\"none\" pattern=\"@\\*\"/d' /etc/ImageMagick-6/policy.xml\n","\n","# Install moviepy\n","!pip install moviepy\n","\n","from moviepy.editor import VideoFileClip, ImageClip, TextClip, CompositeVideoClip\n","import moviepy.config as mp_config\n","import os\n","\n","# Configure moviepy to use ImageMagick binary\n","mp_config.IMAGEMAGICK_BINARY = '/usr/bin/convert'\n","\n","# Function to create a pixelated title clip from the first frame\n","def create_title_clip(input_file, output_file, title_text, duration=3.0, pixelation_factor=0.1):\n"," # Check if input file exists\n"," if not os.path.exists(input_file):\n"," print(f\"Error: Input file {input_file} not found. Skipping.\")\n"," return\n"," try:\n"," # Load only the first frame to minimize memory usage\n"," clip = VideoFileClip(input_file)\n"," first_frame = clip.get_frame(0) # Get the first frame as a numpy array\n"," clip.close() # Close immediately to free memory\n","\n"," # Create a 3-second clip from the first frame\n"," frozen_clip = ImageClip(first_frame, duration=duration)\n","\n"," # Apply pixelation effect to simulate blur (reduce resolution and upscale)\n"," from moviepy.video.fx.all import resize\n"," pixelated_clip = frozen_clip.fx(resize, pixelation_factor).fx(resize, 1.0 / pixelation_factor)\n","\n"," # Create text clip for the title with yellow bold text\n"," text_clip = TextClip(\n"," title_text,\n"," fontsize=28, # Kept for wrapping\n"," color='yellow', # Simple yellow color\n"," font='Arial-Bold', # Bold font\n"," size=(1200, 800), # Narrower width for wrapping\n"," method='caption', # Wrap text automatically\n"," align='center' # Center-align text\n"," ).set_position('center').set_duration(duration)\n","\n"," # Composite the pixelated clip and text\n"," final_clip = CompositeVideoClip([pixelated_clip, text_clip])\n","\n"," # Save the title clip\n"," final_clip.write_videofile(output_file, codec='libx264', audio=False, fps=24)\n"," final_clip.close()\n"," print(f\"Created title clip: {output_file}\")\n"," except Exception as e:\n"," print(f\"Error creating title clip for {input_file}: {e}\")\n","\n","# Input MP4 files and corresponding titles\n","video_files = ['pumpkin5.mp4', 'pumpkin6.mp4', 'pumpkin7.mp4', 'pumpkin89.mp4', 'pumpkin10.mp4']\n","titles = [\n"," 'The Virus Made Animals Giant: My Friends and I ride them through the forest of death', # Main title\n"," 'Vines burst from the ground and grab the tortoise. Its shell glows blue. The girl commands her wolf to bite the vines while she cuts them with a dagger.',\n"," 'Vines retreat. Mist clears. Giant blue butterflies emerge. The fluttering blue butterflies obstruct the foreground, they fly off. The girl leads her wolf deeper into the forest.',\n"," 'A massive red centipede crawls out, the boy throws a Molotov cocktail from the tortoise shell, burning the centipede with an explosion. The centipede disappears. The girl on the wolf and the boy on the turtle escape deeper into the forest.',\n"," 'They reach the forest exit — an open field with giant rabbits and antelopes under the sunset. The girl smiles toward the horizon.'\n","]\n","\n","# Create title clips (main title uses first video's frame)\n","title_clips = [f\"title_main.mp4\"] + [f\"title_{os.path.splitext(file)[0]}.mp4\" for file in video_files]\n","for i, (input_file, output_file, title) in enumerate(zip(video_files, title_clips, titles)):\n"," create_title_clip(input_file, output_file, title, duration=3.0, pixelation_factor=0.1)"],"metadata":{"id":"Hn_hPAgWn-fE"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Install moviepy if not already installed\n","!pip install moviepy\n","\n","from moviepy.editor import VideoFileClip, concatenate_videoclips\n","import numpy as np\n","import os\n","\n","# Function to normalize audio volume\n","def normalize_audio(clip, target_dBFS=-20.0):\n"," if clip.audio is None:\n"," print(f\"Warning: No audio in clip {clip.filename or 'unknown'}. Skipping normalization.\")\n"," return clip\n"," try:\n"," # Get the audio as an array\n"," audio = clip.audio.to_soundarray()\n"," # Ensure audio is a 2D array (handles mono/stereo)\n"," if audio.ndim == 1:\n"," audio = audio[:, np.newaxis]\n"," # Calculate current dBFS\n"," current_dBFS = 10 * np.log10(np.mean(audio**2) + 1e-10) # Avoid log(0)\n"," # Calculate gain needed to reach target dBFS\n"," gain = target_dBFS - current_dBFS\n"," # Apply gain to audio\n"," return clip.volumex(10 ** (gain / 20.0))\n"," except Exception as e:\n"," print(f\"Error normalizing audio for clip {clip.filename or 'unknown'}: {e}. Skipping normalization.\")\n"," return clip\n","\n","# Input MP4 files and corresponding title clips\n","video_files = ['pumpkin5.mp4', 'pumpkin6.mp4', 'pumpkin7.mp4', 'pumpkin89.mp4', 'pumpkin10.mp4']\n","title_clips = [f\"title_main.mp4\"] + [f\"title_{os.path.splitext(file)[0]}.mp4\" for file in video_files]\n","\n","# Load clips\n","clips = []\n","for i, (title_file, video_file) in enumerate(zip(title_clips, video_files)):\n"," try:\n"," # Check if files exist\n"," if not os.path.exists(title_file):\n"," print(f\"Error: Title file {title_file} not found. Skipping this pair.\")\n"," continue\n"," if not os.path.exists(video_file):\n"," print(f\"Error: Video file {video_file} not found. Skipping this pair.\")\n"," continue\n"," # Load title clip (no audio)\n"," title_clip = VideoFileClip(title_file)\n"," clips.append(title_clip)\n"," # Load and normalize original video\n"," video_clip = VideoFileClip(video_file)\n"," video_clip = normalize_audio(video_clip, target_dBFS=-20.0)\n"," clips.append(video_clip)\n"," except Exception as e:\n"," print(f\"Error loading {title_file} or {video_file}: {e}. Skipping this pair.\")\n"," continue\n","\n","# Check if any clips were loaded\n","if not clips:\n"," raise ValueError(\"No valid clips were loaded. Please check your files.\")\n","\n","# Concatenate all clips (no fade transitions)\n","final_sequence = clips # Sequence: title_main, pumpkin5, title_pumpkin6, pumpkin6, ...\n","final_video = concatenate_videoclips(final_sequence, method=\"compose\")\n","\n","# Ensure audio fades for the entire video\n","final_video = final_video.audio_fadein(1.24).audio_fadeout(1.24)\n","\n","# Save the final video\n","output_path = \"output_video.mp4\"\n","try:\n"," final_video.write_videofile(output_path, codec=\"libx264\", audio_codec=\"aac\")\n"," print(f\"Video saved as {output_path}\")\n","except Exception as e:\n"," print(f\"Error saving video: {e}\")\n","\n","# Close clips to free memory\n","for clip in clips:\n"," clip.close()\n","final_video.close()"],"metadata":{"id":"PpO71Peum38c"},"execution_count":null,"outputs":[]}],"metadata":{"colab":{"provenance":[{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Mp4 merge.ipynb","timestamp":1761480798787},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Drive to WebP.ipynb","timestamp":1761335712919},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Drive to WebP.ipynb","timestamp":1760993725927},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1760450712160},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1756712618300},{"file_id":"https://huggingface.co/codeShare/JupyterNotebooks/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1747490904984},{"file_id":"https://huggingface.co/codeShare/JupyterNotebooks/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1740037333374},{"file_id":"https://huggingface.co/codeShare/JupyterNotebooks/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1736477078136},{"file_id":"https://huggingface.co/codeShare/JupyterNotebooks/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1725365086834}]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0}
|