dtup3446 2019-02-10 16:33
浏览 206
已采纳

使用Go图像库从stdout解码bmp图像

I am trying to decode a bmp image using the image and golang.org/x/image/bmp libraries. The image is output by ffmpeg into stdout. This is the code to get the frame:

cmd := exec.Command("ffmpeg", "-accurate_seek", "-ss", strconv.Itoa(index), "-i",
    filename, "-frames:v", "1", "-hide_banner", "-loglevel", "0", "pipe:.bmp")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
    log.Fatal(err)
}

o := bufio.NewReader(&out)

and then I decode it using img, _, err := image.Decode(o) However this gives an error of "image: unknown format". I have already registered the bmp format in the main method, and I have successfully decoded actual BMP files from disk previously, just not from stdout.

I have tried just using bmp.Decode instead of image.Decode but this just gives the error "EOF".

I thought maybe I was not getting the stdout in the correct way, but if I just write it straight to a file:

o := bufio.NewReader(&out)

outputfile, err := os.Create("test.bmp")
if err != nil {
    log.Fatal(err)
}
defer outputfile.Close()
io.Copy(outputfile, o)

then it works fine and I can open it.

Edit: code

  • 写回答

1条回答 默认 最新

  • douyao3895 2019-02-10 23:56
    关注

    Update: turns out the issue was -ss takes time not frame index.

    I tried to reproduce the issue but it seems to work for me. Maybe add cmd.Stderr = os.Stderr to see if ffmpeg output gives come clue. Could you also post a fully runnable example?

    package main
    
    import (
        "bytes"
        "image"
        "log"
        "os"
        "os/exec"
    
        _ "golang.org/x/image/bmp"
    )
    
    func main() {
        cmd := exec.Command(
            "docker", "run",
            "--rm",
            "mwader/static-ffmpeg",
            "-ss", "5",
            "-f", "lavfi",
            "-i", "testsrc",
            "-frames:v", "1",
            "-f", "image2",
            "-vcodec", "bmp",
            "pipe:1",
        )
        var out bytes.Buffer
        cmd.Stderr = os.Stderr
        cmd.Stdout = &out
        err := cmd.Run()
        if err != nil {
            log.Fatal(err)
        }
    
        img, imgFormat, imgErr := image.Decode(&out)
    
        log.Printf("img.Bounds(): %#+v
    ", img.Bounds())
        log.Printf("imgFormat: %#+v
    ", imgFormat)
        log.Printf("imgErr: %#+v
    ", imgErr)
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码