示例#1
0
文件: video.py 项目: esernaalonso/dev
def encode_for_streaming(video_file, scales=["full"]):
    """ Encodes the given video using ffmpeg for streaming purposes.

        Args:
            scales (list of strings, optional): Sizes to encode. Allowed values - "full", "half", "third", "quarter"
    """
    allowed_scales = {"full": 1.0, "threequarters": 0.75, "twothirds":(2.0/3.0), "half": 0.5, "third": (1.0/3.0), "quarter": 0.25}

    if video_file:
        video_size = get_video_size(video_file)
        if video_size:
            for scale in scales:
                if scale in allowed_scales:
                    new_width = int(video_size["width"]*allowed_scales[scale])
                    new_height = int(video_size["height"]*allowed_scales[scale])

                    new_width = new_width if new_width % 2 == 0 else new_width + 1
                    new_height = new_height if new_height % 2 == 0 else new_height + 1

                    new_video_file = "%s_%s.mp4" % (os.path.splitext(video_file)[0], scale)
                    ffmpeg_options = ["-i", video_file, "-c:a", "aac", "-strict", "-2", "-b:a", "128k", "-c:v", "libx264", "-profile:v", "baseline", "-filter:v", ("scale=%s:%s" % (new_width, new_height)), new_video_file]

                    result = ffmpeg.run(ffmpeg_options, exe_name="ffmpeg.exe")

                    logger.info(("Encoded video -> %s" % new_video_file), level=0)
                    logger.info(("Result: %s" % result), level=1)
        else:
            logger.error(("Error getting the original video size -> %s" % video_file), level=0)
示例#2
0
文件: video.py 项目: esernaalonso/dev
def get_video_info(video_file, json_format=True):
    # ffmpeg_options = ["-i", video_file]
    ffmpeg_options = ["-v", "quiet", "-print_format", "json", "-show_format", "-show_streams", video_file]
    video_info = ffmpeg.run(ffmpeg_options, exe_name="ffprobe.exe")
    if json_format:
        try:
            video_info = json.loads(video_info)
        except Exception as e:
            logger.warning(("Error Parsing Video Info -> %s" % e), level=0)
            return None

    return video_info