{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Create Video!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pip install opencv-python opencv-python-headless # If you do not need GUI features\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating the demo\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-08-05 19:36:09,033 - INFO - Found 911 PNG files.\n", "2024-08-05 19:36:09,035 - INFO - Starting video creation...\n", "2024-08-05 19:36:24,399 - INFO - FFmpeg output:\n", "\n", "2024-08-05 19:36:24,400 - INFO - Video created successfully: output_production.mp4\n", "2024-08-05 19:36:24,483 - INFO - Video properties:\n", "codec_name=hevc\n", "width=1024\n", "height=1024\n", "duration=0.160000\n", "\n", "2024-08-05 19:36:25,344 - INFO - Video playback check passed.\n" ] } ], "source": [ "import os\n", "import subprocess\n", "import logging\n", "from glob import glob\n", "\n", "# Configure logging\n", "logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')\n", "logger = logging.getLogger(__name__)\n", "\n", "def create_h265_video(input_folder, output_file, fps=30, frames_per_image=3):\n", " if not os.path.exists(input_folder):\n", " logger.error(f\"Input folder '{input_folder}' does not exist.\")\n", " return\n", "\n", " png_files = sorted(glob(os.path.join(input_folder, '*.png')))\n", " if not png_files:\n", " logger.error(f\"No PNG files found in {input_folder}\")\n", " return\n", "\n", " num_images = len(png_files)\n", " logger.info(f\"Found {num_images} PNG files.\")\n", "\n", " # Calculate expected duration\n", " expected_duration = (num_images * frames_per_image) / fps\n", " logger.info(f\"Expected duration: {expected_duration:.2f} seconds\")\n", "\n", " # Create a temporary file list\n", " with open('temp_file_list.txt', 'w') as f:\n", " for file in png_files:\n", " f.write(f\"file '{file}'\\n\")\n", " f.write(f\"duration {frames_per_image/fps:.6f}\\n\")\n", "\n", " # FFmpeg command to create video\n", " ffmpeg_command = [\n", " 'ffmpeg',\n", " '-f', 'concat',\n", " '-safe', '0',\n", " '-i', 'temp_file_list.txt',\n", " '-vsync', 'vfr',\n", " '-pix_fmt', 'yuv420p', # Ensure compatible color space\n", " '-color_range', '1', # Ensure full color range\n", " '-color_primaries', '1',\n", " '-color_trc', '1',\n", " '-colorspace', '1',\n", " '-c:v', 'libx265',\n", " '-crf', '23',\n", " '-preset', 'medium',\n", " '-tag:v', 'hvc1',\n", " '-y',\n", " output_file\n", " ]\n", "\n", " try:\n", " logger.info(\"Starting video creation...\")\n", " result = subprocess.run(ffmpeg_command, check=True, capture_output=True, text=True)\n", " logger.info(f\"FFmpeg output:\\n{result.stdout}\")\n", " \n", " if os.path.exists(output_file):\n", " logger.info(f\"Video created successfully: {output_file}\")\n", " \n", " # Verify the output\n", " probe_command = ['ffprobe', '-v', 'error', '-show_entries', 'stream=codec_name,width,height,duration', '-of', 'default=noprint_wrappers=1', output_file]\n", " probe_result = subprocess.run(probe_command, capture_output=True, text=True)\n", " logger.info(f\"Video properties:\\n{probe_result.stdout}\")\n", " \n", " # Check if the video is playable and has correct duration\n", " play_check_command = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', output_file]\n", " try:\n", " duration_result = subprocess.run(play_check_command, check=True, capture_output=True, text=True)\n", " actual_duration = float(duration_result.stdout.strip())\n", " logger.info(f\"Actual video duration: {actual_duration:.2f} seconds\")\n", " if abs(actual_duration - expected_duration) > 1: # Allow 1 second tolerance\n", " logger.warning(f\"Video duration mismatch. Expected: {expected_duration:.2f}, Actual: {actual_duration:.2f}\")\n", " else:\n", " logger.info(\"Video duration check passed.\")\n", " except subprocess.CalledProcessError:\n", " logger.error(\"Video duration check failed.\")\n", " else:\n", " logger.error(f\"Output file was not created: {output_file}\")\n", " \n", " except subprocess.CalledProcessError as e:\n", " logger.error(f\"Error during video creation: {e}\")\n", " logger.error(f\"FFmpeg error output:\\n{e.stderr}\")\n", " \n", " finally:\n", " # Clean up\n", " if os.path.exists('temp_file_list.txt'):\n", " os.remove('temp_file_list.txt')\n", "\n", "if __name__ == \"__main__\":\n", " input_folder = 'train'\n", " output_file = 'output_production.mp4'\n", " fps = 30\n", " frames_per_image = 3\n", "\n", " create_h265_video(input_folder, output_file, fps, frames_per_image)\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.14" } }, "nbformat": 4, "nbformat_minor": 2 }