Skip to content

Commit 8879890

Browse files
committed
Refactored framerate
1 parent c4dd9bb commit 8879890

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

pkg/ffmpeg/frame.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func NewFrame(par *Par) (*Frame, error) {
5353
frame.SetWidth(par.Width())
5454
frame.SetHeight(par.Height())
5555
frame.SetSampleAspectRatio(par.SampleAspectRatio())
56-
frame.SetTimeBase(ff.AVUtil_rational_invert(par.Framerate()))
56+
frame.SetTimeBase(par.timebase)
5757
default:
5858
ff.AVUtil_frame_free(frame)
5959
return nil, errors.New("invalid codec type")

pkg/ffmpeg/par.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
type Par struct {
2020
ff.AVCodecParameters
21+
timebase ff.AVRational
2122
}
2223

2324
///////////////////////////////////////////////////////////////////////////////
@@ -76,7 +77,7 @@ func NewVideoPar(pixfmt string, size string, framerate float64) (*Par, error) {
7677
if framerate < 0 {
7778
return nil, ErrBadParameter.Withf("negative framerate %v", framerate)
7879
} else {
79-
par.SetFramerate(ff.AVUtil_rational_d2q(framerate, 1<<24))
80+
par.timebase = ff.AVUtil_rational_invert(ff.AVUtil_rational_d2q(framerate, 1<<24))
8081
}
8182

8283
// Set default sample aspect ratio
@@ -152,7 +153,8 @@ func (ctx *Par) WidthHeight() string {
152153
}
153154

154155
func (ctx *Par) FrameRate() float64 {
155-
return ff.AVUtil_rational_q2d(ctx.Framerate())
156+
framerate := ff.AVUtil_rational_invert(ctx.timebase)
157+
return ff.AVUtil_rational_q2d(framerate)
156158
}
157159

158160
func (ctx *Par) ValidateFromCodec(codec *ff.AVCodec) error {
@@ -250,8 +252,7 @@ func (ctx *Par) copyVideoCodec(codec *ff.AVCodecContext) error {
250252
codec.SetWidth(ctx.Width())
251253
codec.SetHeight(ctx.Height())
252254
codec.SetSampleAspectRatio(ctx.SampleAspectRatio())
253-
codec.SetFramerate(ctx.Framerate())
254-
codec.SetTimeBase(ff.AVUtil_rational_invert(ctx.Framerate()))
255+
codec.SetTimeBase(ctx.timebase)
255256
return nil
256257
}
257258

@@ -265,9 +266,9 @@ func (ctx *Par) validateVideoCodec(codec *ff.AVCodec) error {
265266
ctx.SetPixelFormat(pixelformats[0])
266267
}
267268
}
268-
if ctx.Framerate().Num() == 0 || ctx.Framerate().Den() == 0 {
269+
if ctx.timebase.Num() == 0 || ctx.timebase.Den() == 0 {
269270
if len(framerates) > 0 {
270-
ctx.SetFramerate(framerates[0])
271+
ctx.timebase = ff.AVUtil_rational_invert(framerates[0])
271272
}
272273
}
273274

@@ -286,18 +287,18 @@ func (ctx *Par) validateVideoCodec(codec *ff.AVCodec) error {
286287
if ctx.SampleAspectRatio().Num() == 0 || ctx.SampleAspectRatio().Den() == 0 {
287288
ctx.SetSampleAspectRatio(ff.AVUtil_rational(1, 1))
288289
}
289-
if ctx.Framerate().Num() == 0 || ctx.Framerate().Den() == 0 {
290+
if ctx.timebase.Num() == 0 || ctx.timebase.Den() == 0 {
290291
return ErrBadParameter.With("framerate not set")
291292
} else if len(framerates) > 0 {
292293
valid := false
293294
for _, fr := range framerates {
294-
if ff.AVUtil_rational_equal(fr, ctx.Framerate()) {
295+
if ff.AVUtil_rational_equal(fr, ff.AVUtil_rational_invert(ctx.timebase)) {
295296
valid = true
296297
break
297298
}
298299
}
299300
if !valid {
300-
return ErrBadParameter.Withf("unsupported framerate %v", ctx.Framerate())
301+
return ErrBadParameter.Withf("unsupported framerate %v", ff.AVUtil_rational_invert(ctx.timebase))
301302
}
302303
}
303304

pkg/generator/ebu.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
media "github.com/mutablelogic/go-media"
1515
fonts "github.com/mutablelogic/go-media/etc/fonts"
1616
ffmpeg "github.com/mutablelogic/go-media/pkg/ffmpeg"
17-
ff "github.com/mutablelogic/go-media/sys/ffmpeg61"
1817
)
1918

2019
////////////////////////////////////////////////////////////////////////////
@@ -45,7 +44,7 @@ func NewEBU(par *ffmpeg.Par) (*ebu, error) {
4544
if par.Type() != media.VIDEO {
4645
return nil, errors.New("invalid codec type")
4746
}
48-
framerate := ff.AVUtil_rational_q2d(par.Framerate())
47+
framerate := par.FrameRate()
4948
if framerate <= 0 {
5049
return nil, errors.New("invalid framerate")
5150
}

pkg/generator/yuv420p.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func NewYUV420P(par *ffmpeg.Par) (*yuv420p, error) {
3333
} else if par.PixelFormat() != ff.AV_PIX_FMT_YUV420P {
3434
return nil, errors.New("invalid pixel format, only yuv420p is supported")
3535
}
36-
framerate := ff.AVUtil_rational_q2d(par.Framerate())
36+
framerate := par.FrameRate()
3737
if framerate <= 0 {
3838
return nil, errors.New("invalid framerate")
3939
}

0 commit comments

Comments
 (0)