diff --git a/scripts/helpers/video.py b/scripts/helpers/video.py index f148cd5..44b3e5c 100755 --- a/scripts/helpers/video.py +++ b/scripts/helpers/video.py @@ -3,44 +3,78 @@ import os import sys from datetime import datetime -if __name__ == '__main__': - args = sys.argv[1:] - if len(args) < 2: - print('Usage: compress [rename/cpu/gpu/cpucmd/gpucmd] [crf=24] [additional args]') - # Command to rename all screen recordings - if args[0] == 'rename': - for file in os.listdir('.'): - if file.startswith('Screen Recording ') or file.startswith('Screen Shot '): - pre = 'Rec' if 'Recording' in file else 'Shot' - endIndex = (file.index('AM') if 'AM' in file else file.index('PM')) + 2 - datestr = file[17 if pre == 'Rec' else 12:endIndex] - dt = datetime.strptime(datestr, '%Y-%m-%d at %I.%M.%S %p') - date = dt.strftime(f'{pre} %Y-%m-%d %H-%M' + file[endIndex:]) +def comp(input: str = 'latest', proc: str = 'cpu', codec: str = 'x264', crf: int = 24, + cmd: bool = False, aargs: str = '', suffix: str = 'mp4'): + """ + Compress video - print(f'Renaming {file} to {date}') - os.rename(file, date) + :param input: Input file (Default: latest) + :param proc: cpu (c) or gpu (g) + :param codec: x264 (4) or x265 (5) + :param crf: CRF (quality) for cpu encoding + :param cmd: Whether to output command directly + :param aargs: Additional args + :param suffix: File suffix (Default mp4) + :return: + """ + if input == 'latest': + rename() + i = sorted([s for s in os.listdir('.') if s.startswith('Rec') and s.endswith('mov')])[-1] + else: + i = input - exit() + proc = proc[0] + codec = {'4': {'c': 'x264', 'g': 'h264'}, '5': {'c': 'x265', 'g': 'hevc'}}[codec[-1]][proc] - processor = args[0].lower().strip() - i = args[1] - crf = args[2] if len(args) > 2 else '24' - out = i[:i.rindex('.')] + f'.{processor[0]}265' - cmd = 'cmd' in processor - if cmd: - processor = processor.replace('cmd', '') - if processor == 'cpu': + out = i[:i.rindex('.')] + f'.{codec}' + if proc == 'c': out += f'-{crf}' - out += '.mp4' - additional_args = ' '.join(args[3:] if len(args) > 3 else []) + out += f'.{suffix}' - if processor == 'cpu': - c = f'ffmpeg -i "{i}" -vcodec libx265 -crf {crf} {additional_args} "{out}"' - if processor == 'gpu': - c = f'ffmpeg -i "{i}" -c:v hevc_videotoolbox -b:v 1000k {additional_args} "{out}"' + if proc == 'c': + c = f'ffmpeg -i "{i}" -vcodec lib{codec} -crf {crf} {aargs} "{out}"' + elif proc == 'g': + c = f'ffmpeg -i "{i}" -c:v {codec}_videotoolbox -b:v 1000k {aargs} "{out}"' + else: + raise AssertionError(f'Processor is invalid ({codec}[0] not in "cg")') if cmd: print(c) else: - os.system() \ No newline at end of file + os.system(c) + + +def rename(): + for file in os.listdir('.'): + if file.startswith('Screen Recording ') or file.startswith('Screen Shot '): + pre = 'Rec' if 'Recording' in file else 'Shot' + end_index = (file.index('AM') if 'AM' in file else file.index('PM')) + 2 + datestr = file[17 if pre == 'Rec' else 12:end_index] + dt = datetime.strptime(datestr, '%Y-%m-%d at %I.%M.%S %p') + date = dt.strftime(f'{pre} %Y-%m-%d %H-%M' + file[end_index:]) + + print(f'Renaming {file} to {date}') + os.rename(file, date) + + +if __name__ == '__main__': + if not hasattr(sys, 'ps1'): + args = sys.argv[1:] + if len(args) < 1: + print('Usage: compress [rename/python code]') + + # Command to rename all screen recordings + if args[0] == 'rename': + rename() + exit() + + # processor = args[0].lower().strip() + # i = args[1] + # crf = args[2] if len(args) > 2 else '24' + # cmd = 'cmd' in processor + # if cmd: + # processor = processor.replace('cmd', '') + + # additional_args = ' '.join(args[3:] if len(args) > 3 else []) + print(eval(' '.join(args[0:]))) diff --git a/scripts/zshrc.sh b/scripts/zshrc.sh index be31727..90e8afa 100755 --- a/scripts/zshrc.sh +++ b/scripts/zshrc.sh @@ -119,6 +119,7 @@ cut() { ffmpeg -i $1 -codec copy -ss $start -t $2 Cut\ $1 } alias vcomp="$SCR/helpers/video.py" +alias vcompy="ipython -i $SCR/helpers/video.py" # include if it exists [ -f $HOME/extra.rc.sh ] && . $HOME/extra.rc.sh