简体   繁体   English

有没有办法提供视频部分,提供开始结束秒数?

[英]Is there a way to serve the video part, providing start end seconds?

I am trying to use the backend to serve the video from the storage.我正在尝试使用后端来提供存储中的视频。 I use Go + GIN It works but I need to implement video requests with start and end parameters.我使用 Go + GIN 它可以工作,但我需要使用开始和结束参数来实现视频请求。 For example, I have a video with 10 mins duration and I want to request a fragment from 2 to 3 mins.例如,我有一个时长为 10 分钟的视频,我想请求一个 2 到 3 分钟的片段。 Is it possible or are there examples somewhere?有可能吗?或者在某处有例子吗?

This is what I have now:这就是我现在所拥有的:

accessKeyID := ""
secretAccessKey := ""
useSSL := false

ctx := context.Background()
endpoint := "127.0.0.1:9000"
bucketName := "mybucket"

// Initialize minio client object.
minioClient, err := minio.New(endpoint, &minio.Options{
    Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
    Secure: useSSL,
})
if err != nil {
    log.Fatalln(err)
}

// Get file
object, err := minioClient.GetObject(ctx, bucketName, "1.mp4", minio.GetObjectOptions{})
if err != nil {
    fmt.Println(err)
    return
}
objInfo, err := object.Stat()
if err != nil {
    return
}

buffer := make([]byte, objInfo.Size)
object.Read(buffer)

c.Writer.Header().Set("Content-Length", fmt.Sprintf("%d", objInfo.Size))
c.Writer.Header().Set("Content-Type", "video/mp4")
c.Writer.Header().Set("Connection", "keep-alive")
c.Writer.Header().Set("Content-Range", fmt.Sprintf("bytes 0-%d/%d", objInfo.Size, objInfo.Size))

//c.Writer.Write(buffer)
c.DataFromReader(200, objInfo.Size, "video/mp4", bytes.NewReader(buffer), nil)

This will require your program to at least demux the media stream to get time information out of it, in case you're using a container that supports that, or to actually decode the video stream in case it doesn't - in general, you can't know how many bytes you need to seek into a video file to go to a specific location¹.这将要求您的程序至少解复用媒体 stream 以从中获取时间信息,以防您使用的是支持它的容器,或者实际解码视频 stream 以防它不支持 - 通常,您不知道您需要多少字节才能将视频文件搜索到 go 到特定位置¹。

As the output again needs to be a valid media container so that whoever requested it can deal with it, there's going to be remixing into an output container.由于 output 再次需要成为一个有效的媒体容器,以便任何请求它的人都可以处理它,因此将重新混合到 output 容器中。

So, pick yourself a library that can do that and read its documentation.所以,选择一个可以做到这一点的库并阅读它的文档。 Ffmpeg / avlib is the classical choice there, but I have positively no idea about whether someone else has already written go bindings for it. Ffmpeg / avlib 是那里的经典选择,但我完全不知道其他人是否已经为它编写了 go 绑定。 If not, doing that works be worthwhile.如果没有,这样做是值得的。


¹ there is cases where you can, that would probably apply to MPEG Transport Streams with a fixed mux bitrate. ¹ 在某些情况下,您可以这样做,这可能适用于具有固定多路复用比特率的 MPEG 传输流。 But unless you're working in streaming of video for actual TV towers or actual TV satellites that need a constant rate data stream, you will not likely be dealing with these但是除非您正在为需要恒定速率数据 stream 的实际电视塔或实际电视卫星处理视频流,否则您不太可能处理这些

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM