Created
October 7, 2021 04:20
-
-
Save xjasonlyu/2c739884824bc96b14c0b77df0f51d70 to your computer and use it in GitHub Desktop.
FFMPEG: H264 -> H265
This file contains 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 os | |
import sys | |
video_ext = (".mp4", ".wmv", ".avi", ".rm", ".rmvb", ".m4v", | |
".ogg", ".mkv", ".flv", ".mov", ".3gp", ".ts", ".mpg") | |
def isvideo(n: str) -> bool: | |
_, ext = os.path.splitext(n) | |
return ext.lower() in video_ext | |
def transcode(v_path: str, t_dir: str) -> int: | |
v_path = os.path.abspath(v_path) | |
v_name = os.path.basename(v_path) | |
if not isvideo(v_name): | |
raise NameError(f'{v_name} is not a video file!') | |
t_path = os.path.join(t_dir, v_name) | |
if os.path.exists(t_path): | |
raise FileExistsError(f'{t_path} file exists!') | |
command = 'ffmpeg -hide_banner -i {0} -c:v libx265 -vtag hvc1 -c:a copy {1}'.format( | |
v_path, t_path) | |
return os.system(command=command) | |
def main(): | |
res = 0 | |
try: | |
_, v_path, t_dir = sys.argv | |
res = transcode(v_path, t_dir) | |
except KeyboardInterrupt: | |
pass | |
except ValueError: | |
print('Usage: {} video_path target_dir'.format( | |
os.path.basename(sys.argv[0]))) | |
except Exception as e: | |
print(e) | |
exit(res) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment