Jordan Legg commited on
Commit
69938b4
·
1 Parent(s): 5c012db

update: finalised prod demo and python file

Browse files
create-video.ipynb CHANGED
@@ -25,39 +25,21 @@
25
  },
26
  {
27
  "cell_type": "code",
28
- "execution_count": 1,
29
  "metadata": {},
30
- "outputs": [
31
- {
32
- "name": "stderr",
33
- "output_type": "stream",
34
- "text": [
35
- "2024-08-05 19:36:09,033 - INFO - Found 911 PNG files.\n",
36
- "2024-08-05 19:36:09,035 - INFO - Starting video creation...\n",
37
- "2024-08-05 19:36:24,399 - INFO - FFmpeg output:\n",
38
- "\n",
39
- "2024-08-05 19:36:24,400 - INFO - Video created successfully: output_production.mp4\n",
40
- "2024-08-05 19:36:24,483 - INFO - Video properties:\n",
41
- "codec_name=hevc\n",
42
- "width=1024\n",
43
- "height=1024\n",
44
- "duration=0.160000\n",
45
- "\n",
46
- "2024-08-05 19:36:25,344 - INFO - Video playback check passed.\n"
47
- ]
48
- }
49
- ],
50
  "source": [
51
  "import os\n",
52
  "import subprocess\n",
53
  "import logging\n",
54
  "from glob import glob\n",
 
55
  "\n",
56
  "# Configure logging\n",
57
  "logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')\n",
58
  "logger = logging.getLogger(__name__)\n",
59
  "\n",
60
- "def create_h265_video(input_folder, output_file, fps=30, frames_per_image=3):\n",
61
  " if not os.path.exists(input_folder):\n",
62
  " logger.error(f\"Input folder '{input_folder}' does not exist.\")\n",
63
  " return\n",
@@ -74,83 +56,196 @@
74
  " expected_duration = (num_images * frames_per_image) / fps\n",
75
  " logger.info(f\"Expected duration: {expected_duration:.2f} seconds\")\n",
76
  "\n",
77
- " # Create a temporary file list\n",
78
- " with open('temp_file_list.txt', 'w') as f:\n",
79
- " for file in png_files:\n",
80
- " f.write(f\"file '{file}'\\n\")\n",
81
- " f.write(f\"duration {frames_per_image/fps:.6f}\\n\")\n",
82
- "\n",
83
- " # FFmpeg command to create video\n",
84
  " ffmpeg_command = [\n",
85
  " 'ffmpeg',\n",
86
- " '-f', 'concat',\n",
87
- " '-safe', '0',\n",
88
- " '-i', 'temp_file_list.txt',\n",
89
- " '-vsync', 'vfr',\n",
90
- " '-pix_fmt', 'yuv420p', # Ensure compatible color space\n",
91
- " '-color_range', '1', # Ensure full color range\n",
92
- " '-color_primaries', '1',\n",
93
- " '-color_trc', '1',\n",
94
- " '-colorspace', '1',\n",
95
- " '-c:v', 'libx265',\n",
96
- " '-crf', '23',\n",
97
- " '-preset', 'medium',\n",
98
  " '-tag:v', 'hvc1',\n",
99
  " '-y',\n",
100
  " output_file\n",
101
  " ]\n",
102
  "\n",
103
  " try:\n",
104
- " logger.info(\"Starting video creation...\")\n",
105
- " result = subprocess.run(ffmpeg_command, check=True, capture_output=True, text=True)\n",
106
- " logger.info(f\"FFmpeg output:\\n{result.stdout}\")\n",
107
  " \n",
108
- " if os.path.exists(output_file):\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  " logger.info(f\"Video created successfully: {output_file}\")\n",
110
  " \n",
111
- " # Verify the output\n",
112
- " probe_command = ['ffprobe', '-v', 'error', '-show_entries', 'stream=codec_name,width,height,duration', '-of', 'default=noprint_wrappers=1', output_file]\n",
113
  " probe_result = subprocess.run(probe_command, capture_output=True, text=True)\n",
114
  " logger.info(f\"Video properties:\\n{probe_result.stdout}\")\n",
115
  " \n",
116
- " # Check if the video is playable and has correct duration\n",
117
- " play_check_command = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', output_file]\n",
118
- " try:\n",
119
- " duration_result = subprocess.run(play_check_command, check=True, capture_output=True, text=True)\n",
120
- " actual_duration = float(duration_result.stdout.strip())\n",
121
- " logger.info(f\"Actual video duration: {actual_duration:.2f} seconds\")\n",
122
- " if abs(actual_duration - expected_duration) > 1: # Allow 1 second tolerance\n",
123
- " logger.warning(f\"Video duration mismatch. Expected: {expected_duration:.2f}, Actual: {actual_duration:.2f}\")\n",
124
- " else:\n",
125
- " logger.info(\"Video duration check passed.\")\n",
126
- " except subprocess.CalledProcessError:\n",
127
- " logger.error(\"Video duration check failed.\")\n",
128
  " else:\n",
129
- " logger.error(f\"Output file was not created: {output_file}\")\n",
130
- " \n",
131
  " except subprocess.CalledProcessError as e:\n",
132
  " logger.error(f\"Error during video creation: {e}\")\n",
133
  " logger.error(f\"FFmpeg error output:\\n{e.stderr}\")\n",
134
- " \n",
135
- " finally:\n",
136
- " # Clean up\n",
137
- " if os.path.exists('temp_file_list.txt'):\n",
138
- " os.remove('temp_file_list.txt')\n",
139
  "\n",
140
  "if __name__ == \"__main__\":\n",
141
  " input_folder = 'train'\n",
142
- " output_file = 'output_production.mp4'\n",
143
  " fps = 30\n",
144
  " frames_per_image = 3\n",
 
145
  "\n",
146
- " create_h265_video(input_folder, output_file, fps, frames_per_image)\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  "\n",
 
 
 
148
  "\n",
 
 
 
 
149
  "\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  "\n",
 
151
  "\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  "\n",
153
- "\n"
154
  ]
155
  }
156
  ],
 
25
  },
26
  {
27
  "cell_type": "code",
28
+ "execution_count": null,
29
  "metadata": {},
30
+ "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  "source": [
32
  "import os\n",
33
  "import subprocess\n",
34
  "import logging\n",
35
  "from glob import glob\n",
36
+ "import re\n",
37
  "\n",
38
  "# Configure logging\n",
39
  "logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')\n",
40
  "logger = logging.getLogger(__name__)\n",
41
  "\n",
42
+ "def create_near_lossless_h265_video(input_folder, output_file, fps=30, frames_per_image=3, crf=10):\n",
43
  " if not os.path.exists(input_folder):\n",
44
  " logger.error(f\"Input folder '{input_folder}' does not exist.\")\n",
45
  " return\n",
 
56
  " expected_duration = (num_images * frames_per_image) / fps\n",
57
  " logger.info(f\"Expected duration: {expected_duration:.2f} seconds\")\n",
58
  "\n",
59
+ " # FFmpeg command for near-lossless 10-bit H.265 encoding\n",
 
 
 
 
 
 
60
  " ffmpeg_command = [\n",
61
  " 'ffmpeg',\n",
62
+ " '-framerate', f'{1/(frames_per_image/fps)}', # Input framerate\n",
63
+ " '-i', os.path.join(input_folder, '%*.png'), # Input pattern for all PNG files\n",
64
+ " '-fps_mode', 'vfr',\n",
65
+ " '-pix_fmt', 'yuv420p10le', # 10-bit pixel format\n",
66
+ " '-c:v', 'libx265', # Use libx265 encoder\n",
67
+ " '-preset', 'slow', # Slowest preset for best compression efficiency\n",
68
+ " '-crf', str(crf), # Constant Rate Factor (0-51, lower is higher quality)\n",
69
+ " '-profile:v', 'main10', # 10-bit profile\n",
70
+ " '-x265-params', f\"log-level=error:keyint={2*fps}:min-keyint={fps}:scenecut=0\", # Ensure consistent encoding\n",
 
 
 
71
  " '-tag:v', 'hvc1',\n",
72
  " '-y',\n",
73
  " output_file\n",
74
  " ]\n",
75
  "\n",
76
  " try:\n",
77
+ " logger.info(\"Starting near-lossless 10-bit video creation...\")\n",
78
+ " process = subprocess.Popen(ffmpeg_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)\n",
 
79
  " \n",
80
+ " encoding_speed = None\n",
81
+ " \n",
82
+ " for line in process.stderr:\n",
83
+ " print(line, end='') # Print FFmpeg output in real-time\n",
84
+ " \n",
85
+ " speed_match = re.search(r'speed=\\s*([\\d.]+)x', line)\n",
86
+ " if speed_match:\n",
87
+ " encoding_speed = float(speed_match.group(1))\n",
88
+ " \n",
89
+ " process.wait()\n",
90
+ " \n",
91
+ " if encoding_speed:\n",
92
+ " logger.info(f\"Encoding speed: {encoding_speed:.2f}x\")\n",
93
+ " \n",
94
+ " if process.returncode == 0:\n",
95
  " logger.info(f\"Video created successfully: {output_file}\")\n",
96
  " \n",
97
+ " probe_command = ['ffprobe', '-v', 'error', '-show_entries', 'stream=codec_name,width,height,duration,bit_rate,profile', '-of', 'default=noprint_wrappers=1', output_file]\n",
 
98
  " probe_result = subprocess.run(probe_command, capture_output=True, text=True)\n",
99
  " logger.info(f\"Video properties:\\n{probe_result.stdout}\")\n",
100
  " \n",
101
+ " duration_command = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', output_file]\n",
102
+ " duration_result = subprocess.run(duration_command, capture_output=True, text=True)\n",
103
+ " actual_duration = float(duration_result.stdout.strip())\n",
104
+ " logger.info(f\"Actual video duration: {actual_duration:.2f} seconds\")\n",
105
+ " if abs(actual_duration - expected_duration) > 1:\n",
106
+ " logger.warning(f\"Video duration mismatch. Expected: {expected_duration:.2f}, Actual: {actual_duration:.2f}\")\n",
107
+ " else:\n",
108
+ " logger.info(\"Video duration check passed.\")\n",
 
 
 
 
109
  " else:\n",
110
+ " logger.error(f\"Error during video creation. FFmpeg returned code {process.returncode}\")\n",
111
+ "\n",
112
  " except subprocess.CalledProcessError as e:\n",
113
  " logger.error(f\"Error during video creation: {e}\")\n",
114
  " logger.error(f\"FFmpeg error output:\\n{e.stderr}\")\n",
 
 
 
 
 
115
  "\n",
116
  "if __name__ == \"__main__\":\n",
117
  " input_folder = 'train'\n",
118
+ " output_file = 'near_lossless_output.mp4'\n",
119
  " fps = 30\n",
120
  " frames_per_image = 3\n",
121
+ " crf = 18 # Very low CRF for near-lossless quality (0 is lossless, but often overkill)\n",
122
  "\n",
123
+ " create_near_lossless_h265_video(input_folder, output_file, fps, frames_per_image, crf)\n"
124
+ ]
125
+ },
126
+ {
127
+ "cell_type": "markdown",
128
+ "metadata": {},
129
+ "source": [
130
+ "## Transfer File"
131
+ ]
132
+ },
133
+ {
134
+ "cell_type": "code",
135
+ "execution_count": null,
136
+ "metadata": {},
137
+ "outputs": [],
138
+ "source": [
139
+ "import os\n",
140
+ "import subprocess\n",
141
+ "import logging\n",
142
+ "from glob import glob\n",
143
+ "import re\n",
144
  "\n",
145
+ "# Configure logging\n",
146
+ "logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')\n",
147
+ "logger = logging.getLogger(__name__)\n",
148
  "\n",
149
+ "def create_high_quality_video(input_folder, output_file, fps=60, frames_per_image=3, codec='ffv1'):\n",
150
+ " if not os.path.exists(input_folder):\n",
151
+ " logger.error(f\"Input folder '{input_folder}' does not exist.\")\n",
152
+ " return\n",
153
  "\n",
154
+ " png_files = sorted(glob(os.path.join(input_folder, '*.png')))\n",
155
+ " if not png_files:\n",
156
+ " logger.error(f\"No PNG files found in {input_folder}\")\n",
157
+ " return\n",
158
+ "\n",
159
+ " num_images = len(png_files)\n",
160
+ " logger.info(f\"Found {num_images} PNG files.\")\n",
161
+ "\n",
162
+ " # Calculate expected duration\n",
163
+ " expected_duration = (num_images * frames_per_image) / fps\n",
164
+ " logger.info(f\"Expected duration: {expected_duration:.2f} seconds\")\n",
165
+ "\n",
166
+ " # Base FFmpeg command\n",
167
+ " ffmpeg_command = [\n",
168
+ " 'ffmpeg',\n",
169
+ " '-framerate', f'{1/(frames_per_image/fps)}', # Input framerate\n",
170
+ " '-i', os.path.join(input_folder, '%*.png'), # Input pattern for all PNG files\n",
171
+ " '-fps_mode', 'vfr',\n",
172
+ " ]\n",
173
+ "\n",
174
+ " # Codec-specific settings\n",
175
+ " if codec == 'ffv1':\n",
176
+ " output_file = output_file.rsplit('.', 1)[0] + '.mkv' # FFV1 is typically used with MKV container\n",
177
+ " ffmpeg_command.extend([\n",
178
+ " '-c:v', 'ffv1',\n",
179
+ " '-level', '3',\n",
180
+ " '-coder', '1',\n",
181
+ " '-context', '1',\n",
182
+ " '-g', '1',\n",
183
+ " '-slices', '24',\n",
184
+ " '-slicecrc', '1'\n",
185
+ " ])\n",
186
+ " logger.info(\"Using FFV1 codec (lossless)\")\n",
187
+ " elif codec == 'prores':\n",
188
+ " output_file = output_file.rsplit('.', 1)[0] + '.mov' # ProRes is typically used with MOV container\n",
189
+ " ffmpeg_command.extend([\n",
190
+ " '-c:v', 'prores_ks',\n",
191
+ " '-profile:v', '4444', # Highest quality ProRes\n",
192
+ " '-qscale:v', '5' # Lower values mean higher quality, 5 is very high quality\n",
193
+ " ])\n",
194
+ " logger.info(\"Using ProRes codec (near-lossless)\")\n",
195
+ " else:\n",
196
+ " logger.error(f\"Unsupported codec: {codec}\")\n",
197
+ " return\n",
198
  "\n",
199
+ " ffmpeg_command.extend(['-y', output_file])\n",
200
  "\n",
201
+ " try:\n",
202
+ " logger.info(f\"Starting high-quality video creation with {codec} codec...\")\n",
203
+ " process = subprocess.Popen(ffmpeg_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)\n",
204
+ " \n",
205
+ " encoding_speed = None\n",
206
+ " \n",
207
+ " for line in process.stderr:\n",
208
+ " print(line, end='') # Print FFmpeg output in real-time\n",
209
+ " \n",
210
+ " speed_match = re.search(r'speed=\\s*([\\d.]+)x', line)\n",
211
+ " if speed_match:\n",
212
+ " encoding_speed = float(speed_match.group(1))\n",
213
+ " \n",
214
+ " process.wait()\n",
215
+ " \n",
216
+ " if encoding_speed:\n",
217
+ " logger.info(f\"Encoding speed: {encoding_speed:.4f}x\")\n",
218
+ " \n",
219
+ " if process.returncode == 0:\n",
220
+ " logger.info(f\"Video created successfully: {output_file}\")\n",
221
+ " \n",
222
+ " probe_command = ['ffprobe', '-v', 'error', '-show_entries', 'stream=codec_name,width,height,duration,bit_rate', '-of', 'default=noprint_wrappers=1', output_file]\n",
223
+ " probe_result = subprocess.run(probe_command, capture_output=True, text=True)\n",
224
+ " logger.info(f\"Video properties:\\n{probe_result.stdout}\")\n",
225
+ " \n",
226
+ " duration_command = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', output_file]\n",
227
+ " duration_result = subprocess.run(duration_command, capture_output=True, text=True)\n",
228
+ " actual_duration = float(duration_result.stdout.strip())\n",
229
+ " logger.info(f\"Actual video duration: {actual_duration:.2f} seconds\")\n",
230
+ " if abs(actual_duration - expected_duration) > 1:\n",
231
+ " logger.warning(f\"Video duration mismatch. Expected: {expected_duration:.2f}, Actual: {actual_duration:.2f}\")\n",
232
+ " else:\n",
233
+ " logger.info(\"Video duration check passed.\")\n",
234
+ " else:\n",
235
+ " logger.error(f\"Error during video creation. FFmpeg returned code {process.returncode}\")\n",
236
+ "\n",
237
+ " except subprocess.CalledProcessError as e:\n",
238
+ " logger.error(f\"Error during video creation: {e}\")\n",
239
+ " logger.error(f\"FFmpeg error output:\\n{e.stderr}\")\n",
240
+ "\n",
241
+ "if __name__ == \"__main__\":\n",
242
+ " input_folder = 'train'\n",
243
+ " output_file = 'high_quality_output.mp4'\n",
244
+ " fps = 60\n",
245
+ " frames_per_image = 3\n",
246
+ " codec = 'prores' # Options: 'ffv1' (lossless) or 'prores' (near-lossless)\n",
247
  "\n",
248
+ " create_high_quality_video(input_folder, output_file, fps, frames_per_image, codec)"
249
  ]
250
  }
251
  ],
demo.mp4 → high_quality_output.mov RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:0b1e78e0a7690bfe40ccfe88d31d220c04cc43fc06e1c3c303ca97556ca96306
3
- size 156352481
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:513358f3f492c5122c2a2c66e2c507f5ba5cf46741e54f781dcd1ea419f18775
3
+ size 598130197
image_metadata_extraction.ipynb CHANGED
The diff for this file is too large to render. See raw diff
 
video_helpers.py DELETED
@@ -1,13 +0,0 @@
1
- # video_helpers.py
2
- import cv2
3
- import logging
4
-
5
- def write_frames(image, temp_file, fps, frames_per_image, size):
6
- logging.info(f'Processing image and writing to {temp_file}')
7
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
8
- out = cv2.VideoWriter(temp_file, fourcc, fps, size)
9
-
10
- for _ in range(frames_per_image):
11
- out.write(image)
12
-
13
- out.release()