Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

h264视频文件获取frame后如何保存为图片? #73

Open
mediawall opened this issue May 3, 2023 · 7 comments
Open

h264视频文件获取frame后如何保存为图片? #73

mediawall opened this issue May 3, 2023 · 7 comments

Comments

@mediawall
Copy link

mediawall commented May 3, 2023

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"

	"github.com/yapingcat/gomedia/go-mpeg2"
)

func main() {
	f, _ := os.Open("x.mp4")
	defer f.Close()

	dm := mpeg2.NewTSDemuxer()
	dm.OnFrame = func(cid mpeg2.TS_STREAM_TYPE, frame []byte, pts uint64, dts uint64) {
		if cid == mpeg2.TS_STREAM_H264 {
			println("---1", len(frame), pts, dts)
			// how to convert frame []byte to jpg or png?
		}
	}

	buf, _ := ioutil.ReadAll(f)
	fmt.Printf("read %d size\n", len(buf))
	fmt.Println(dm.Input(bytes.NewReader(buf)))
}

抱歉啊,搜索了半天没有找到解决办法。对视频图像了解实在有限,请教下是不是可行,谢谢!

@c1ngular
Copy link

c1ngular commented May 4, 2023

需要解码,你可能需要的是ffmpeg

@mediawall
Copy link
Author

就是想着怎么能避免安装很大的ffmpeg

我的视频ffpobe 结果是:

Input #0, flv, from '4.flv':
  Duration: N/A, start: 0.000000, bitrate: N/A
  Stream #0:0: Video: h264 (High), yuv420p(progressive), 1280x720, 1k tbr, 1k tbn
Input #0, mpegts, from 'x.ts':
  Duration: 00:00:06.00, start: 20.034000, bitrate: 314 kb/s
  Program 1 
    Metadata:
      service_name    : Service01
      service_provider: FFmpeg
  Stream #0:0[0x100]: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuvj420p(pc, bt709, progressive), 352x288, 30 tbr, 90k tbn

@yapingcat
Copy link
Owner

yapingcat commented May 4, 2023

纯golang做不到
需要先解码再编码,ffmpeg比较合适
或者用cgo调用ffmpeg的library

@lab37
Copy link

lab37 commented Feb 4, 2024

能不能实现只把H264的关键帧保存成图片。这样会不会简单一些。不太想引入ffmpeg,Cgo有点拉跨

@yapingcat
Copy link
Owner

能不能实现只把H264的关键帧保存成图片。这样会不会简单一些。不太想引入ffmpeg,Cgo有点拉跨

实现是是一样的,都需要先解码再编码

@yapingcat
Copy link
Owner

能不能实现只把H264的关键帧保存成图片。这样会不会简单一些。不太想引入ffmpeg,Cgo有点拉跨

不想用cgo,可以试试ffmepg 管道模式. 或者开发一个简单转码服务,go程序用socket/zmq 其他进程间通信方式与之交互,传输h264

@lab37
Copy link

lab37 commented Feb 4, 2024

能不能实现只把H264的关键帧保存成图片。这样会不会简单一些。不太想引入ffmpeg,Cgo有点拉跨

不想用cgo,可以试试ffmepg 管道模式. 或者开发一个简单转码服务,go程序用socket/zmq 其他进程间通信方式与之交互,传输h264

好的,谢谢解答

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants