ffmpeg: add Bitrate and profile

This commit is contained in:
nareix 2016-07-16 12:36:21 +08:00
parent 37c9a0c41e
commit 30e62bbdfb
3 changed files with 32 additions and 6 deletions

View File

@ -157,7 +157,7 @@ func (self *Resampler) Close() {
type AudioEncoder struct {
ff *ffctx
SampleRate int
BitRate int
Bitrate int
ChannelLayout av.ChannelLayout
SampleFormat av.SampleFormat
FrameSampleCount int
@ -233,10 +233,25 @@ func (self *AudioEncoder) SetChannelLayout(ch av.ChannelLayout) (err error) {
return
}
func (self *AudioEncoder) SetBitrate(bitrate int) (err error) {
self.Bitrate = bitrate
return
}
func (self *AudioEncoder) SetOption(key string, val interface{}) (err error) {
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
}
@ -268,8 +283,8 @@ func (self *AudioEncoder) Setup() (err error) {
self.SampleFormat = sampleFormatFF2AV(*ff.codec.sample_fmts)
}
if self.BitRate == 0 {
self.BitRate = 80000
if self.Bitrate == 0 {
self.Bitrate = 80000
}
if self.SampleRate == 0 {
self.SampleRate = 44100
@ -280,10 +295,11 @@ func (self *AudioEncoder) Setup() (err error) {
ff.codecCtx.sample_fmt = sampleFormatAV2FF(self.SampleFormat)
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.strict_std_compliance = C.FF_COMPLIANCE_EXPERIMENTAL
ff.codecCtx.flags = C.AV_CODEC_FLAG_GLOBAL_HEADER
ff.codecCtx.profile = ff.profile
if C.avcodec_open2(ff.codecCtx, ff.codec, nil) != 0 {
err = fmt.Errorf("ffmpeg: encoder: avcodec_open2 failed")

View File

@ -52,6 +52,7 @@ func newFFCtxByCodec(codec *C.AVCodec) (ff *ffctx, err error) {
ff = &ffctx{}
ff.ff.codec = codec
ff.ff.codecCtx = C.avcodec_alloc_context3(codec)
ff.ff.profile = C.FF_PROFILE_UNKNOWN
runtime.SetFinalizer(ff, freeFFCtx)
return
}

View File

@ -11,5 +11,14 @@ typedef struct {
AVCodecContext *codecCtx;
AVFrame *frame;
AVDictionary *options;
int profile;
} 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;
}