mirror of
https://github.com/qrtc/ffmpeg-dev-go.git
synced 2025-10-05 15:47:33 +08:00
2023-10-31 09:16:44 CST W44D2
This commit is contained in:
@@ -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 {
|
||||
|
Reference in New Issue
Block a user