从视频字节获取帧

时间:2019-09-07 23:03:21

标签: go ffmpeg

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

我正在从文件读取数据,但是我不确定如何从字节读取数据。

我看过ffmpeg文档here,但只看到输出示例,而不是输入示例。

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 个答案:

答案 0 :(得分:3)

通过指定do { System.out.println("1. Add item"); //<-- where are 2-6? choice = sc.nextLine().charAt(0); switch (choice) { case '6': // <-- don't forget case '1' - '5' try { CarSales.ReadData(); } catch (IOException e) { System.out.println("Error reading file '"); } continue; // <-- here, or a break; default: System.out.println("Invalid Selection\n"); } } while (choice != '6'); 作为选项ffmpeg的值,可以使-从stdin读取数据,而不是从磁盘读取文件。然后只需将您的视频字节作为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() } 来确定ffmpeg版本中是否支持ffmpeg -protocols协议(从stdin读取)。