def copy_exif_to_file(settings: Settings, original_file_path: str, new_file_path: str, media_file: MediaFile): args = [ settings.path_exiftool, "-overwrite_original", "-tagsFromFile", original_file_path, new_file_path ] if ScannedFileType.VIDEO.name == media_file.file_type and media_file.video_rotation: args.insert(4, "-rotation={}".format(media_file.video_rotation)) if ScannedFileType.IMAGE.name == media_file.file_type and media_file.extension in [ "HEIC", "HEIF" ]: args.insert(4, "-x") args.insert(5, "Orientation") MiscUtils.exec_subprocess(args, "EXIF copy failed")
def convert_image_file(settings: Settings, media_file: MediaFile, original_file_path: str, save_file_path: str): # Sample: magick convert -resize 320x480 -quality 75 inputFile.cr2 outputfile.jpg args = [ settings.path_magick, "convert", "-quality", str(settings.image_compression_quality), "{}[0]".format(original_file_path), save_file_path ] new_dimentions = MediaProcessor.get_new_dimentions( media_file.height, media_file.width, settings.image_max_dimension) if new_dimentions: args.insert(2, "-resize") args.insert( 3, "{}x{}".format(new_dimentions['height'], new_dimentions['width'])) MiscUtils.exec_subprocess(args, "Image conversion failed")
def convert_video_file(settings: Settings, media_file: MediaFile, original_file_path: str, new_file_path: str, target_gpu: int): new_dimentions = MediaProcessor.get_new_dimentions( media_file.height, media_file.width, settings.video_max_dimension) audio_bitrate_arg = str(settings.video_audio_bitrate) + "k" # Adding ability to play converted videos in QuickTime: https://brandur.org/fragments/ffmpeg-h265 if target_gpu < 0: # CPU Sample: ffmpeg -noautorotate -i input -c:v libx265 -crf 28 -tag:v hvc1 -c:a aac -ac 2 -vf scale=320:240 -b:a 128k -y output.mp4 args = [ settings.path_ffmpeg, "-noautorotate", "-i", original_file_path, "-c:v", "libx265", "-crf", str(settings.video_crf), "-tag:v", "hvc1", "-c:a", "aac", "-ac", "2", "-b:a", audio_bitrate_arg, "-y", new_file_path ] if new_dimentions: args.insert(14, "-vf") args.insert( 15, "scale={}:{}".format(new_dimentions['width'], new_dimentions['height'])) else: # GPU Sample: ffmpeg -noautorotate -vsync 0 -hwaccel cuda -hwaccel_device 0 -hwaccel_output_format cuda -i input -c:v hevc_nvenc -preset medium -rc vbr -cq 38 -gpu 0 -c:a aac -ac 2 -b:a 128k -tag:v hvc1 -vf scale_cuda=2560:1440 -y output.mp4 args = [ settings.path_ffmpeg, "-noautorotate", "-vsync", "0", "-hwaccel", "cuda", "-hwaccel_device", str(target_gpu), "-hwaccel_output_format", "cuda", "-i", original_file_path, "-c:v", "hevc_nvenc", "-preset", settings.video_nvenc_preset, "-rc", "vbr", "-cq", str(settings.video_crf), "-gpu", str(target_gpu), "-c:a", "aac", "-ac", "2", "-b:a", audio_bitrate_arg, "-tag:v", "hvc1", "-y", new_file_path ] if new_dimentions: args.insert(30, "-vf") args.insert( 31, "scale_cuda={}:{}".format(new_dimentions['width'], new_dimentions['height'])) MiscUtils.exec_subprocess(args, "Video conversion failed")