ffmpeg: add Bitrate and profile
This commit is contained in:
parent
37c9a0c41e
commit
30e62bbdfb
@ -157,7 +157,7 @@ func (self *Resampler) Close() {
|
|||||||
type AudioEncoder struct {
|
type AudioEncoder struct {
|
||||||
ff *ffctx
|
ff *ffctx
|
||||||
SampleRate int
|
SampleRate int
|
||||||
BitRate int
|
Bitrate int
|
||||||
ChannelLayout av.ChannelLayout
|
ChannelLayout av.ChannelLayout
|
||||||
SampleFormat av.SampleFormat
|
SampleFormat av.SampleFormat
|
||||||
FrameSampleCount int
|
FrameSampleCount int
|
||||||
@ -233,10 +233,25 @@ func (self *AudioEncoder) SetChannelLayout(ch av.ChannelLayout) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *AudioEncoder) SetBitrate(bitrate int) (err error) {
|
||||||
|
self.Bitrate = bitrate
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (self *AudioEncoder) SetOption(key string, val interface{}) (err error) {
|
func (self *AudioEncoder) SetOption(key string, val interface{}) (err error) {
|
||||||
ff := &self.ff.ff
|
ff := &self.ff.ff
|
||||||
s := fmt.Sprint(val)
|
|
||||||
C.av_dict_set(&ff.options, C.CString(key), C.CString(s), 0)
|
sval := fmt.Sprint(val)
|
||||||
|
if key == "profile" {
|
||||||
|
ff.profile = C.avcodec_profile_name_to_int(ff.codec, C.CString(sval))
|
||||||
|
if ff.profile == C.FF_PROFILE_UNKNOWN {
|
||||||
|
err = fmt.Errorf("ffmpeg: profile `%s` invalid", sval)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
C.av_dict_set(&ff.options, C.CString(key), C.CString(sval), 0)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,8 +283,8 @@ func (self *AudioEncoder) Setup() (err error) {
|
|||||||
self.SampleFormat = sampleFormatFF2AV(*ff.codec.sample_fmts)
|
self.SampleFormat = sampleFormatFF2AV(*ff.codec.sample_fmts)
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.BitRate == 0 {
|
if self.Bitrate == 0 {
|
||||||
self.BitRate = 80000
|
self.Bitrate = 80000
|
||||||
}
|
}
|
||||||
if self.SampleRate == 0 {
|
if self.SampleRate == 0 {
|
||||||
self.SampleRate = 44100
|
self.SampleRate = 44100
|
||||||
@ -280,10 +295,11 @@ func (self *AudioEncoder) Setup() (err error) {
|
|||||||
|
|
||||||
ff.codecCtx.sample_fmt = sampleFormatAV2FF(self.SampleFormat)
|
ff.codecCtx.sample_fmt = sampleFormatAV2FF(self.SampleFormat)
|
||||||
ff.codecCtx.sample_rate = C.int(self.SampleRate)
|
ff.codecCtx.sample_rate = C.int(self.SampleRate)
|
||||||
ff.codecCtx.bit_rate = C.int64_t(self.BitRate)
|
ff.codecCtx.bit_rate = C.int64_t(self.Bitrate)
|
||||||
ff.codecCtx.channel_layout = channelLayoutAV2FF(self.ChannelLayout)
|
ff.codecCtx.channel_layout = channelLayoutAV2FF(self.ChannelLayout)
|
||||||
ff.codecCtx.strict_std_compliance = C.FF_COMPLIANCE_EXPERIMENTAL
|
ff.codecCtx.strict_std_compliance = C.FF_COMPLIANCE_EXPERIMENTAL
|
||||||
ff.codecCtx.flags = C.AV_CODEC_FLAG_GLOBAL_HEADER
|
ff.codecCtx.flags = C.AV_CODEC_FLAG_GLOBAL_HEADER
|
||||||
|
ff.codecCtx.profile = ff.profile
|
||||||
|
|
||||||
if C.avcodec_open2(ff.codecCtx, ff.codec, nil) != 0 {
|
if C.avcodec_open2(ff.codecCtx, ff.codec, nil) != 0 {
|
||||||
err = fmt.Errorf("ffmpeg: encoder: avcodec_open2 failed")
|
err = fmt.Errorf("ffmpeg: encoder: avcodec_open2 failed")
|
||||||
|
@ -52,6 +52,7 @@ func newFFCtxByCodec(codec *C.AVCodec) (ff *ffctx, err error) {
|
|||||||
ff = &ffctx{}
|
ff = &ffctx{}
|
||||||
ff.ff.codec = codec
|
ff.ff.codec = codec
|
||||||
ff.ff.codecCtx = C.avcodec_alloc_context3(codec)
|
ff.ff.codecCtx = C.avcodec_alloc_context3(codec)
|
||||||
|
ff.ff.profile = C.FF_PROFILE_UNKNOWN
|
||||||
runtime.SetFinalizer(ff, freeFFCtx)
|
runtime.SetFinalizer(ff, freeFFCtx)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -11,5 +11,14 @@ typedef struct {
|
|||||||
AVCodecContext *codecCtx;
|
AVCodecContext *codecCtx;
|
||||||
AVFrame *frame;
|
AVFrame *frame;
|
||||||
AVDictionary *options;
|
AVDictionary *options;
|
||||||
|
int profile;
|
||||||
} FFCtx;
|
} FFCtx;
|
||||||
|
|
||||||
|
static inline int avcodec_profile_name_to_int(AVCodec *codec, const char *name) {
|
||||||
|
const AVProfile *p;
|
||||||
|
for (p = codec->profiles; p != NULL && p->profile != FF_PROFILE_UNKNOWN; p++)
|
||||||
|
if (!strcmp(p->name, name))
|
||||||
|
return p->profile;
|
||||||
|
return FF_PROFILE_UNKNOWN;
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user