-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhap-encoder.py
More file actions
56 lines (48 loc) · 2.26 KB
/
hap-encoder.py
File metadata and controls
56 lines (48 loc) · 2.26 KB
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
import os
import subprocess
import argparse
def main():
parser = argparse.ArgumentParser(description='Convert MP4 videos to HAP format.')
parser.add_argument('input_dir', help='Path to the input directory containing MP4 files')
parser.add_argument('output_dir', help='Path to the output directory for HAP MOV files')
parser.add_argument('--hap-format', choices=['hap', 'hap_q', 'hap_alpha'], default='hap', help='HAP Format variant (default: hap)')
parser.add_argument('--overwrite', action='store_true', help='Overwrite existing output files without asking')
args = parser.parse_args()
# validate input directory
if not os.path.isdir(args.input_dir):
print(f"Error: Input directory '{args.input_dir}' does not exist")
return
# create output directory if doesn't exist
os.makedirs(args.output_dir, exist_ok=True)
# process files
valid_ext = ('.mp4', '.mov')
for filename in os.listdir(args.input_dir):
if not filename.lower().endswith(valid_ext):
continue
input_path = os.path.join(args.input_dir, filename)
output_filename = os.path.splitext(filename)[0] + '.mov'
output_path = os.path.join(args.output_dir, output_filename)
# skip existing files if overwrite disabled
if os.path.exists(output_path) and not args.overwrite:
print(f'Skipping {filename} (output already exists)')
continue
# ffmpeg command
cmd = [
'ffmpeg',
'-i', input_path,
'-c:v', 'hap',
'-format', args.hap_format,
'-y', # always overwrite (we handle skipping in the script)
output_path
]
# Run convserion
try:
print(f'Converting {filename} to HAP {args.hap_format}...')
subprocess.run(cmd, check=True, capture_output=True)
print(f'Successfully converted {filename}')
except subprocess.CalledProcessError as e:
print(f'Error converting {filename}: {e.stderr.decode()}')
except Exception as e:
print(f'Unexpected error processing {filename}: {str(e)}')
if __name__ == '__main__':
main()