视频处理基础之gradio框架实现

这些函数是用于处理视频文件的Python代码片段,它们依赖于`ffmpeg`和`ffprobe`工具,这些工具是`FFmpeg`项目的一部分,用于处理视频和音频数据。下面是每个函数的用途和用法的总结:

1. `ffmpeg_installed()` 函数:
   - 用途:检查系统是否安装了`ffmpeg`工具。
   - 用法:调用此函数无需参数,它会返回一个布尔值,指示`ffmpeg`是否可用。

2. `video_is_playable(video_filepath: str)` 函数:
   - 用途:确定视频文件是否可以在浏览器中播放。
   - 用法:传入视频文件的路径作为字符串,函数会检查视频的容器格式和编解码器是否符合浏览器的播放标准(如`.mp4`容器使用`h264`编解码器,`.webm`容器使用`vp9`编解码器等)。如果视频符合标准,则返回`True`,否则返回`False`。

3. `convert_video_to_playable_mp4(video_path: str)` 函数:
   - 用途:将视频转换为浏览器可以播放的MP4格式。
   - 用法:传入视频文件的路径作为字符串,函数会尝试将视频转换为MP4格式。如果转换过程中出现错误,则返回原始视频路径。

4. `get_video_length(video_path: str | Path)` 函数:
   - 用途:获取视频文件的时长。
   - 用法:传入视频文件的路径,函数会使用`ffprobe`工具来获取视频的时长,并以秒为单位返回一个浮点数。

这些函数的实现依赖于外部命令行工具`ffmpeg`和`ffprobe`,因此在使用这些函数之前,需要确保这些工具已经正确安装在系统上。此外,代码中还包含了一些错误处理机制,以确保在处理视频文件时能够优雅地处理异常情况。例如,如果`ffmpeg`或`ffprobe`命令失败,或者视频文件的格式不符合预期,函数会返回原始视频路径或抛出异常。
IS_WASM = sys.platform == "emscripten"

def ffmpeg_installed() -> bool:
    if wasm_utils.IS_WASM:
        # TODO: Support ffmpeg in WASM
        return False

    return shutil.which("ffmpeg") is not None


def video_is_playable(video_filepath: str) -> bool:
    """Determines if a video is playable in the browser.

    A video is playable if it has a playable container and codec.
        .mp4 -> h264
        .webm -> vp9
        .ogg -> theora
    """
    from ffmpy import FFprobe, FFRuntimeError

    try:
        container = Path(video_filepath).suffix.lower()
        probe = FFprobe(
            global_options="-show_format -show_streams -select_streams v -print_format json",
            inputs={video_filepath: None},
        )
        output = probe.run(stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        output = json.loads(output[0])
        video_codec = output["streams"][0]["codec_name"]
        return (container, video_codec) in [
            (".mp4", "h264"),
            (".ogg", "theora"),
            (".webm", "vp9"),
        ]
    # If anything goes wrong, assume the video can be played to not convert downstream
    except (FFRuntimeError, IndexError, KeyError):
        return True


def convert_video_to_playable_mp4(video_path: str) -> str:
    """Convert the video to mp4. If something goes wrong return the original video."""
    from ffmpy import FFmpeg, FFRuntimeError

    try:
        with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
            output_path = Path(video_path).with_suffix(".mp4")
            shutil.copy2(video_path, tmp_file.name)
            # ffmpeg will automatically use h264 codec (playable in browser) when converting to mp4
            ff = FFmpeg(
                inputs={str(tmp_file.name): None},
                outputs={str(output_path): None},
                global_options="-y -loglevel quiet",
            )
            ff.run()
    except FFRuntimeError as e:
        print(f"Error converting video to browser-playable format {str(e)}")
        output_path = video_path
    finally:
        # Remove temp file
        os.remove(tmp_file.name)  # type: ignore
    return str(output_path)


def get_video_length(video_path: str | Path):
    if wasm_utils.IS_WASM:
        raise wasm_utils.WasmUnsupportedError(
            "Video duration is not supported in the Wasm mode."
        )
    duration = subprocess.check_output(
        [
            "ffprobe",
            "-i",
            str(video_path),
            "-show_entries",
            "format=duration",
            "-v",
            "quiet",
            "-of",
            "csv={}".format("p=0"),
        ]
    )
    duration_str = duration.decode("utf-8").strip()
    duration_float = float(duration_str)

    return duration_float

  • 20
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李飞刀李寻欢

您的欣赏将是我奋斗路上的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值