Last active
August 23, 2022 18:37
-
-
Save shoffing/ae840db8c0143266f0816d9f0c92e5ef to your computer and use it in GitHub Desktop.
Converts all video files to H265, deleting the sources as well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pathlib | |
import subprocess | |
import re | |
VIDEO_TYPES = [ | |
'.mp4', | |
'.avi', | |
'.mkv', | |
'.mov', | |
'.webm', | |
] | |
PARTIAL_PATTERN = re.compile('.+_x265_part$') | |
PROCESSED_PATTERN = re.compile('.+_x265$') | |
def partial_path(path: pathlib.Path): | |
return path.with_stem(f'{path.stem}_x265_part').with_suffix('.mp4') | |
def processed_path(path: pathlib.Path): | |
return path.with_stem(f'{path.stem}_x265').with_suffix('.mp4') | |
unprocessed_videos = [ | |
path for path in (pathlib.Path.cwd() / 'Takeout').rglob('*') | |
if path.is_file | |
and path.suffix.lower() in VIDEO_TYPES | |
and not PARTIAL_PATTERN.match(path.stem) | |
and not PROCESSED_PATTERN.match(path.stem) | |
and not processed_path(path).exists() | |
] | |
for idx, video_path in enumerate(unprocessed_videos): | |
print(f'\n\n>> Starting video #{idx+1} of {len(unprocessed_videos)}: {video_path}') | |
video_partial_path = partial_path(video_path) | |
if video_partial_path.exists(): | |
print(f'>>>> Found partial video: {video_partial_path}') | |
video_partial_path.unlink() | |
command = [ | |
'ffmpeg', '-xerror', '-hide_banner', | |
# '-loglevel', 'error', | |
'-i', video_path, | |
'-map_metadata', '0', | |
'-c:a', 'libopus', '-b:a', '64k', | |
'-c:v', 'libx265', '-preset', 'veryfast', '-x265-params', 'log-level=error:crf=23', | |
'-c:s', 'copy', | |
video_partial_path, | |
] | |
subprocess.run(command, check=True) | |
video_processed_path = processed_path(video_path) | |
video_partial_path.rename(video_processed_path) | |
video_path.unlink() | |
print(f'>> Processed video #{idx+1} of {len(unprocessed_videos)}: {video_processed_path}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment