从视频字节获取帧

Kel*_*let 2 ffmpeg go

我正在使用 ffmpeg 和 golang 从视频中提取一帧。如果我有一个以字节为单位的视频,而不是作为 .mp4 保存在磁盘上,我如何告诉 ffmpeg 从这些字节中读取而不必将文件写入磁盘,因为这要慢得多?

我从文件中读取了这个工作,但我不确定如何从字节中读取。

我在这里查看了ffmpeg文档但只看到了输出示例而不是输入示例。

func ExtractImage(fileBytes []byte){

    // command line args, path, and command
    command = "ffmpeg"
    frameExtractionTime := "0:00:05.000"
    vframes := "1"
    qv := "2"
    output := "/home/ubuntu/media/video-to-image/output-" + time.Now().Format(time.Kitchen) + ".jpg"

    // TODO: use fileBytes instead of videoPath
    // create the command
    cmd := exec.Command(command,
        "-ss", frameExtractionTime,
        "-i", videoPath,
        "-vframes", vframes,
        "-q:v", qv,
        output)

    // run the command and don't wait for it to finish. waiting exec is run
    // ignore errors for examples-sake
    _ = cmd.Start()
    _ = cmd.Wait()
}
Run Code Online (Sandbox Code Playgroud)

小智 6

ffmpeg通过指定-为 option 的值,您可以从 stdin 读取数据而不是从磁盘读取文件-i。然后只需将您的视频字节作为标准输入传递给命令。

func ExtractImage(fileBytes []byte){

    // command line args, path, and command
    command := "ffmpeg"
    frameExtractionTime := "0:00:05.000"
    vframes := "1"
    qv := "2"
    output := "/home/ubuntu/media/video-to-image/output-" + time.Now().Format(time.Kitchen) + ".jpg"

    cmd := exec.Command(command,
        "-ss", frameExtractionTime,
        "-i", "-",  // to read from stdin
        "-vframes", vframes,
        "-q:v", qv,
        output)

    cmd.Stdin = bytes.NewBuffer(fileBytes)

    // run the command and don't wait for it to finish. waiting exec is run
    // ignore errors for examples-sake
    _ = cmd.Start()
    _ = cmd.Wait()
}
Run Code Online (Sandbox Code Playgroud)

您可能需要运行ffmpeg -protocols以确定pipe您的 ffmpeg 构建中是否支持协议(从 stdin 读取)。