-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffprobe.go
82 lines (66 loc) · 2.04 KB
/
ffprobe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package mediaprobe
/*
#cgo pkg-config: libavutil
#include <stdlib.h>
#include "libavutil/pixdesc.h"
*/
import "C"
import (
"time"
"github.com/3d0c/gmf"
)
// FFProbe parses video or audio using ffmpeg bindings
// It uses github.com/3d0c/gmf package
func (info *Info) FFProbe() error {
inputCtx, err := gmf.NewInputCtx(info.filename)
if err != nil {
return err
}
defer inputCtx.Free()
info.Duration = time.Duration(inputCtx.Duration() * float64(time.Second))
info.StartTime = time.Duration(inputCtx.StartTime() * int(time.Second))
info.BitRate = inputCtx.BitRate()
for idx := 0; idx < inputCtx.StreamsCnt(); idx++ {
stream, err := inputCtx.GetStream(idx)
if err != nil {
return err
}
codecCtx := stream.CodecCtx()
if codecCtx == nil {
// do nothing if can't extract codec from stream
continue
}
codec := codecCtx.Codec()
streamInfo := Stream{
ID: stream.Id(),
Index: stream.Index(),
Bitrate: codecCtx.BitRate(),
MediaType: codecCtx.GetMediaType(),
Codec: codec.Name(),
CodecLongName: codec.LongName(),
CodecTag: codecCtx.GetCodecTagName(),
IsExperimental: codec.IsExperimental(),
SampleFmtName: codecCtx.GetSampleFmtName(),
}
if stream.IsVideo() {
streamInfo.Width = codecCtx.Width()
streamInfo.Height = codecCtx.Height()
streamInfo.FrameRate = stream.GetRFrameRate().AVR().Av2qd()
streamInfo.AvgFrameRate = stream.GetAvgFrameRate().AVR().Av2qd()
streamInfo.Profile = codecCtx.GetProfileName()
streamInfo.ColorRangeName = codecCtx.GetColorRangeName()
streamInfo.AspectRation = codecCtx.GetAspectRation().AVR().Av2qd()
streamInfo.BFrames = codecCtx.GetBFrames()
streamInfo.BitsPerSample = codecCtx.GetBitsPerSample()
streamInfo.PixFmt = int(codecCtx.PixFmt())
streamInfo.PixFmtName = info.pixFmtName(codecCtx.PixFmt())
}
codecCtx.Free()
stream.Free()
info.Streams = append(info.Streams, streamInfo)
}
return nil
}
func (info *Info) pixFmtName(pixFmt int32) string {
return C.GoString(C.av_get_pix_fmt_name(pixFmt))
}