def save_and_transcode_video(data, extension, file_code): temp_file_path = os.path.join( settings.VIDEOS_FILE_PATH, generate_file_path("{}.{}".format(file_code, extension))) + "_tmp" save_dir = temp_file_path[:temp_file_path.rfind("/") + 1] if not os.path.exists(save_dir): os.makedirs(save_dir, mode=0o777) with open(temp_file_path, "wb") as f: f.write(data) long_file_name = "{}.{}".format(file_code, settings.VIDEO_OUTPUT_EXTENSION) save_file_path = os.path.join(settings.VIDEOS_FILE_PATH, generate_file_path(long_file_name)) ffmpeg\ .input(temp_file_path)\ .filter("scale", height=settings.VIDEO_OUTPUT_HEIGHT, width="trunc(oh*a/2)*2")\ .output(save_file_path, **settings.VIDEO_OUTPUT_SETTINGS)\ .run(cmd=settings.FFMPEG_PATH) os.remove(temp_file_path) return long_file_name, save_file_path
def width_fit_media(max_length, filename): if not is_image(filename): return full_media(filename) ok_filepath = generate_file_path(filename) filepath = os.path.join(settings.IMAGES_FILE_PATH, "width/{}/{}".format(max_length, ok_filepath)) if not os.path.exists(filepath): try: image = Image.open( os.path.join(settings.FULL_IMAGE_FILE_PATH, ok_filepath)) except IOError: return "Not found", 404 image_dir = filepath[:filepath.rfind("/") + 1] if not os.path.exists(image_dir): os.makedirs(image_dir, mode=0o777) image_width = float(image.size[0]) image_height = float(image.size[1]) new_width = int(max_length) new_height = int(new_width / image_width * image_height) image.thumbnail((new_width, new_height), Image.ANTIALIAS) image.save(filepath, quality=settings.IMAGE_QUALITY) return x_accel_response("/images/width/{}/{}".format( max_length, ok_filepath))
def square_fit_media(max_length, filename): if not is_image(filename): return full_media(filename) ok_filepath = generate_file_path(filename) filepath = os.path.join(settings.IMAGES_FILE_PATH, "square/{}/{}".format(max_length, ok_filepath)) if not os.path.exists(filepath): try: image = Image.open( os.path.join(settings.FULL_IMAGE_FILE_PATH, ok_filepath)) except IOError: return "Not found", 404 image_dir = filepath[:filepath.rfind("/") + 1] if not os.path.exists(image_dir): os.makedirs(image_dir, mode=0o777) image_width = float(image.size[0]) image_height = float(image.size[1]) image_square = int(min(image_width, image_height)) crop_coordinates_x = int(image_width / 2 - image_square / 2) crop_coordinates_y = int(image_height / 2 - image_square / 2) image = image.crop((crop_coordinates_x, crop_coordinates_y, crop_coordinates_x + image_square, crop_coordinates_y + image_square)) image.thumbnail((max_length, max_length), Image.ANTIALIAS) image.save(filepath, quality=settings.IMAGE_QUALITY) return x_accel_response("/images/square/{}/{}".format( max_length, ok_filepath))
def save_full_image(data, extension, file_code): long_file_name = "{}.{}".format(file_code, extension) save_file_path = os.path.join(settings.FULL_IMAGE_FILE_PATH, generate_file_path(long_file_name)) save_dir = save_file_path[:save_file_path.rfind("/") + 1] if not os.path.exists(save_dir): os.makedirs(save_dir, mode=0o777) file = io.BytesIO(data) image = Image.open(file) image_width = float(image.size[0]) image_height = float(image.size[1]) orig_save_size = get_fit_image_size(image_width, image_height, settings.ORIGINAL_IMAGE_MAX_LENGTH) image.thumbnail(orig_save_size, Image.ANTIALIAS) try: image = auto_rotate_by_exif(image) except (IOError, KeyError, AttributeError) as ex: logger.error("Auto-rotation error: {}".format(ex)) image.save(save_file_path, quality=settings.IMAGE_QUALITY) return long_file_name, save_file_path
def full_media(filename): if is_image(filename): return x_accel_response("/images/max/{}".format( generate_file_path(filename))) return x_accel_response("/videos/{}".format(generate_file_path(filename)))