2023-10-31 09:16:44 CST W44D2

This commit is contained in:
aggresss
2023-10-31 09:16:44 +08:00
parent f00ecdba54
commit 05345f7cdc
100 changed files with 3340 additions and 3229 deletions

View File

@@ -10,7 +10,7 @@ import (
ffmpeg "github.com/qrtc/ffmpeg-dev-go"
)
// check that a given sample format is supported by the encoder
// check that a given sample format is supported by the encoder.
func checkSampleFmt(codec *ffmpeg.AVCodec, sampleFmt ffmpeg.AVSampleFormat) int32 {
for _, f := range codec.GetSampleFmts() {
if f == sampleFmt {
@@ -34,25 +34,28 @@ func selectSampleRate(codec *ffmpeg.AVCodec) int32 {
return bestSamplerate
}
// select layout with the highest channel count
func selectChannelLayout(codec *ffmpeg.AVCodec) uint64 {
var bestChLayout uint64
var bestNbChannels int32
ls := codec.GetChannelLayouts()
if len(ls) == 0 {
return ffmpeg.AV_CH_LAYOUT_STEREO
// select layout with the highest channel count.
func selectChannelLayout(codec *ffmpeg.AVCodec, dst *ffmpeg.AVChannelLayout) int32 {
var (
bestChLayout *ffmpeg.AVChannelLayout
bestNbChannels int32
)
chls := codec.GetChLayouts()
if len(chls) == 0 {
return ffmpeg.AvChannelLayoutCopy(dst, ffmpeg.AV_CHANNEL_LAYOUT_STEREO())
}
for _, l := range ls {
nbChannels := ffmpeg.AvGetChannelLayoutNbChannels(l)
for i := range chls {
nbChannels := chls[i].GetNbChannels()
if nbChannels > bestNbChannels {
bestChLayout = l
bestChLayout = &chls[i]
bestNbChannels = nbChannels
}
}
return bestChLayout
return ffmpeg.AvChannelLayoutCopy(dst, bestChLayout)
}
func encode(ctx *ffmpeg.AVCodecContext, frame *ffmpeg.AVFrame, pkt *ffmpeg.AVPacket, output *os.File) {
@@ -111,8 +114,10 @@ func main() {
// select other audio parameters supported by the encoder
avctx.SetSampleRate(selectSampleRate(codec))
avctx.SetChannelLayout(selectChannelLayout(codec))
avctx.SetChannels(ffmpeg.AvGetChannelLayoutNbChannels(avctx.GetChannelLayout()))
if selectChannelLayout(codec, avctx.GetChLayoutAddr()) < 0 {
fmt.Fprintf(os.Stderr, "codec context select channel layout failed\n")
os.Exit(1)
}
// open it
if ffmpeg.AvCodecOpen2(avctx, codec, nil) < 0 {
@@ -141,7 +146,10 @@ func main() {
frame.SetNbSamples(avctx.GetFrameSize())
frame.SetFormat(avctx.GetSampleFmt())
frame.SetChannelLayout(avctx.GetChannelLayout())
if ffmpeg.AvChannelLayoutCopy(frame.GetChLayoutAddr(), avctx.GetChLayoutAddr()) < 0 {
fmt.Fprintf(os.Stderr, "frame copy channel layout failed\n")
os.Exit(1)
}
// allocate the data buffers
if ret := ffmpeg.AvFrameGetBuffer(frame, 0); ret < 0 {