donglianjiang9321 2019-09-07 23:03 采纳率: 0%
浏览 344
已采纳

从视频字节获取帧

I'm extracting a frame from a video using ffmpeg and golang. If I have a video in bytes instead of saved on disk as an .mp4, how do I tell ffmpeg to read from those bytes without having to write the file to disk, as that is much slower?

I have this working reading from a file, but I'm not sure how to read from bytes.

I've looked at the ffmpeg documentation here but only see output examples instead of input examples.

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()
}
  • 写回答

1条回答 默认 最新

  • dongle0396 2019-09-08 04:07
    关注

    You can make ffmpeg to read data from stdin rather than reading file from disk, by specifying - as the value of the option -i. Then just pass your video bytes as stdin to the command.

    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()
    }
    

    You may need to run ffmpeg -protocols to determine if the pipe protocol (to read from stdin) is supported in your build of ffmpeg.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想做个WPS的自动化代码,不知道能做的起不。
  • ¥15 斯坦福自动漂移非线性模型反解
  • ¥15 学习Python如何找兼职
  • ¥15 python结合Matlab仿真忆阻器
  • ¥35 有人会注册whatsaop协议号吗?
  • ¥15 lead dbs 无法导入影像数据
  • ¥15 多目标MPA算法优化编程实现
  • ¥15 反激PWM控制芯片调研
  • ¥15 Python for loop减少运行时间
  • ¥15 fluent模拟物质浓度udf