-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpegcomp.py
445 lines (413 loc) · 14.4 KB
/
mpegcomp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#
# import os
# import subprocess
# import glob
# import re
# from concurrent.futures import ThreadPoolExecutor
# from typing import Dict, Optional, Callable, Union
#
#
# def raw_to_video(input_path, output_video, width, height, framerate, input_pixel_format, output_pixel_format, qp):
# """
# 将原始视频文件转换为编码视频文件
#
# Args:
# input_path (str): 输入文件路径
# output_video (str): 输出视频文件路径
# width (int): 视频宽度
# height (int): 视频高度
# framerate (int): 帧率
# input_pixel_format (str): 输入像素格式
# output_pixel_format (str): 输出像素格式
# qp (int): 量化参数
# """
# cmd = [
# 'ffmpeg',
# '-f', 'rawvideo',
# '-pix_fmt', input_pixel_format,
# '-video_size', f'{width}x{height}',
# '-framerate', str(framerate),
# '-i', input_path,
# '-pix_fmt', output_pixel_format,
# '-c:v', 'libx265',
# '-qp', str(qp),
# # '-x265-params', 'keyint=999999:min-keyint=999999:scenecut=0',
# # '-keyint_min', '200',
# # '-g', '200',
# '-y',
# output_video
# ]
# try:
# subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# return True
# except subprocess.CalledProcessError as e:
# print(f"视频编码失败 {input_path}: {e}")
# return False
#
#
# def video_to_raw(input_video, output_path, output_pixel_format):
# """
# 将编码视频文件转换回原始格式
#
# Args:
# input_video (str): 输入视频文件路径
# output_path (str): 输出文件路径
# output_pixel_format (str): 输出像素格式
# """
# cmd = [
# 'ffmpeg',
# '-i', input_video,
# '-f', 'rawvideo',
# '-pix_fmt', output_pixel_format,
# '-y',
# output_path
# ]
# try:
# subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# return True
# except subprocess.CalledProcessError as e:
# print(f"解码失败 {input_video}: {e}")
# return False
#
#
# def get_intermediate_format(filename: str, default_format: str = 'gbrp12le') -> str:
# """
# 根据文件名决定使用的中间像素格式
#
# Args:
# filename (str): 文件名
# default_format (str): 默认像素格式
#
# Returns:
# str: 选定的像素格式
# """
# # 使用正则表达式匹配color_1到color_15的模式
# if re.match(r'color_(?:[1-9]|1[0-5])', os.path.splitext(filename)[0]):
# return 'yuv444p12le'
# return default_format
#
# def get_qp(filename: str, default_qp) -> int:
#
# # 使用正则表达式匹配color_1到color_15的模式
# if re.match(r'color_(?:[1-9]|1[0-5])', os.path.splitext(filename)[0]):
# return default_qp
# return 0
#
#
# def process_single_file(
# input_file: str,
# output_dir: str,
# width: int,
# height: int,
# framerate: int,
# input_pixel_format: str,
# output_pixel_format: str,
# d_qp: int,
# default_intermediate_format: str = 'gbrp12le'
# ) -> bool:
# """
# 处理单个文件的完整流程
#
# Args:
# input_file (str): 输入文件路径
# output_dir (str): 输出目录路径
# width (int): 视频宽度
# height (int): 视频高度
# framerate (int): 帧率
# input_pixel_format (str): 输入像素格式
# output_pixel_format (str): 输出像素格式
# qp (int): 量化参数
# default_intermediate_format (str): 默认中间像素格式
#
# Returns:
# bool: 处理是否成功
# """
# base_name = os.path.splitext(os.path.basename(input_file))[0]
# temp_video = os.path.join(output_dir, f"{base_name}_temp.hevc")
# final_output = os.path.join(output_dir, f"{base_name}{os.path.splitext(input_file)[1]}")
#
# # 确定中间像素格式
# intermediate_format = get_intermediate_format(
# os.path.basename(input_file),
# default_intermediate_format
# )
#
# t_qp = get_qp(
# os.path.basename(input_file),
# d_qp
# )
#
# if raw_to_video(input_file, temp_video, width, height, framerate,
# input_pixel_format, intermediate_format, t_qp):
# if video_to_raw(temp_video, final_output, output_pixel_format):
# # 可选:删除临时视频文件
# # os.remove(temp_video)
# return True
# return False
#
#
# def process_files(
# input_dir: str,
# output_dir: str,
# width: int = 150,
# height: int = 70,
# framerate: int = 160,
# input_pixel_format: str = 'rgb48le',
# output_pixel_format: str = 'rgb48le',
# pattern: str = '*.rgb48le',
# max_workers: int = 4,
# qp: int = 30,
# default_intermediate_format: str = 'gbrp12le'
# ) -> None:
# """
# 批量处理文件
#
# Args:
# input_dir (str): 输入目录
# output_dir (str): 输出目录
# width (int): 视频宽度
# height (int): 视频高度
# framerate (int): 帧率
# input_pixel_format (str): 输入像素格式
# output_pixel_format (str): 输出像素格式
# pattern (str): 文件匹配模式
# max_workers (int): 最大并行处理数
# qp (int): 量化参数
# default_intermediate_format (str): 默认中间像素格式
# """
# os.makedirs(output_dir, exist_ok=True)
#
# input_files = glob.glob(os.path.join(input_dir, pattern))
# if not input_files:
# print(f"在 {input_dir} 中没有找到匹配 {pattern} 的文件")
# return
#
# with ThreadPoolExecutor(max_workers=max_workers) as executor:
# futures = [
# executor.submit(
# process_single_file,
# input_file,
# output_dir,
# width,
# height,
# framerate,
# input_pixel_format,
# output_pixel_format,
# qp,
# default_intermediate_format
# )
# for input_file in input_files
# ]
#
# completed = 0
# for future in futures:
# if future.result():
# completed += 1
#
# print(f"处理完成!成功转换 {completed}/{len(input_files)} 个文件")
import os
import subprocess
import glob
import re
from concurrent.futures import ThreadPoolExecutor
from typing import Dict, Optional, Callable, Union
def raw_to_video(input_path, output_video, width, height, framerate, input_pixel_format, output_pixel_format, qp=None):
"""
Convert raw video file to encoded video file with flexible encoding parameters
Args:
input_path (str): Input file path
output_video (str): Output video file path
width (int): Video width
height (int): Video height
framerate (int): Frame rate
input_pixel_format (str): Input pixel format
output_pixel_format (str): Output pixel format
crf (int): Constant Rate Factor for lossless/near-lossless encoding
qp (int): Quantization Parameter
"""
cmd = [
'ffmpeg',
'-f', 'rawvideo',
'-pix_fmt', input_pixel_format,
'-video_size', f'{width}x{height}',
'-framerate', str(framerate),
'-i', input_path,
'-pix_fmt', output_pixel_format,
'-c:v', 'libx265',
]
# Add encoding parameters based on CRF or QP
if qp is None:
cmd.extend(['-x265-params', 'lossless=1'])
# cmd.extend(['-x265-params',"bframes=0:keyint=1",'-qp', '0'])
elif qp > 0:
cmd.extend(['-qp', str(qp)])
else:
# Default to lossless encoding
cmd.extend(['-qp', '0'])
cmd.extend([
'-y',
output_video
])
try:
subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return True
except subprocess.CalledProcessError as e:
print(f"Video encoding failed for {input_path}: {e}")
return False
def video_to_raw(input_video, output_path, output_pixel_format):
"""
Convert encoded video file back to raw format
Args:
input_video (str): Input video file path
output_path (str): Output file path
output_pixel_format (str): Output pixel format
"""
cmd = [
'ffmpeg',
'-i', input_video,
'-f', 'rawvideo',
'-pix_fmt', output_pixel_format,
'-y',
output_path
]
try:
subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return True
except subprocess.CalledProcessError as e:
print(f"Decoding failed for {input_video}: {e}")
return False
def process_directory(
input_dir: str,
output_dir: str,
width: int = 100,
height: int = 100,
framerate: int = 60,
input_pixel_format: str = 'rgb48le',
max_workers: int = 4,
qp: int = None
) -> None:
"""
Process files in different subdirectories with specialized encoding
Args:
input_dir (str): Root input directory
output_dir (str): Root output directory
width (int): Video width
height (int): Video height
framerate (int): Frame rate
input_pixel_format (str): Input pixel format
output_pixel_format (str): Output pixel format
max_workers (int): Maximum parallel processing workers
"""
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
output_pixel_format ={'y':'yuv444p12le','g':'gbrp12le'}
# Process files in each subdirectory
subdirs = ['coord', 'color', 'geo']
for subdir in subdirs:
input_subdir = os.path.join(input_dir, subdir)
output_subdir = os.path.join(output_dir, subdir)
os.makedirs(output_subdir, exist_ok=True)
# Find all RGB files
input_files = sorted(glob.glob(os.path.join(input_subdir, '*.rgb48le')))
if not input_files:
print(f"No files found in {input_subdir}")
continue
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = []
for i, input_file in enumerate(input_files):
base_name = os.path.splitext(os.path.basename(input_file))[0]
temp_video = os.path.join(output_subdir, f"{base_name}_temp.hevc")
final_output = os.path.join(output_subdir, f"{base_name}{os.path.splitext(input_file)[1]}")
# Encoding strategy based on subdirectory
if subdir == 'coord':
# Lossless encoding for coord files
futures.append(
executor.submit(
process_single_file,
input_file, temp_video, final_output,
width, height, framerate,
input_pixel_format, output_pixel_format['g'],
qp=None # Lossless encoding for coord files
)
)
elif subdir == 'color':
# First file CRF 10, others with increased QP
if i == 0:
futures.append(
executor.submit(
process_single_file,
input_file, temp_video, final_output,
width, height, framerate,
input_pixel_format, output_pixel_format['g'],
qp=0
)
)
else:
futures.append(
executor.submit(
process_single_file,
input_file, temp_video, final_output,
width, height, framerate,
input_pixel_format, output_pixel_format['y'],
qp=qp # Increased QP for other files
)
)
elif subdir == 'geo':
futures.append(
executor.submit(
process_single_file,
input_file, temp_video, final_output,
width, height, framerate,
input_pixel_format, output_pixel_format['g'],
qp=0
)
)
# Wait for all futures to complete and track results
completed = sum(1 for future in futures if future.result())
print(f"Processed {subdir}: {completed}/{len(input_files)} files successfully")
def process_single_file(
input_file: str,
temp_video: str,
final_output: str,
width: int,
height: int,
framerate: int,
input_pixel_format: str,
mid_pixel_format: str,
qp: int = None
) -> bool:
"""
Process a single file with specialized encoding
Args:
input_file (str): Input file path
temp_video (str): Temporary video file path
final_output (str): Final output file path
width (int): Video width
height (int): Video height
framerate (int): Frame rate
input_pixel_format (str): Input pixel format
output_pixel_format (str): Output pixel format
Returns:
bool: Processing success status
"""
if raw_to_video(input_file, temp_video, width, height, framerate,
input_pixel_format, mid_pixel_format, qp=qp):
if video_to_raw(temp_video, final_output, 'rgb48le'):
# Optional: Remove temporary video file
# os.remove(temp_video)
return True
return False
def main(input_dir: str, output_dir: str):
"""
Main function to process entire directory structure
Args:
input_dir (str): Root input directory
output_dir (str): Root output directory
"""
process_directory(input_dir, output_dir)
if __name__ == "__main__":
import sys
if len(sys.argv) != 3:
print("Usage: python script.py <input_directory> <output_directory>")
sys.exit(1)
main(sys.argv[1], sys.argv[2])