Skip to content

Commit 6b2b307

Browse files
committed
Various updates
1 parent 0219f88 commit 6b2b307

File tree

16 files changed

+594
-417
lines changed

16 files changed

+594
-417
lines changed

etc/test/sample.jpg

48.8 KB
Loading

etc/test/sample.png

1.45 MB
Loading

pkg/ffmpeg/encoder.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type EncoderFrameFn func(int) (*Frame, error)
3535

3636
// EncoderPacketFn is a function which is called for each packet encoded, with
3737
// the stream timebase.
38-
type EncoderPacketFn func(*ff.AVPacket, *ff.AVRational) error
38+
type EncoderPacketFn func(*Packet) error
3939

4040
////////////////////////////////////////////////////////////////////////////////
4141
// LIFECYCLE
@@ -199,8 +199,6 @@ func (e *Encoder) nextPts(frame *Frame) int64 {
199199
// PRIVATE METHODS
200200

201201
func (e *Encoder) encode(frame *Frame, fn EncoderPacketFn) error {
202-
timebase := e.stream.TimeBase()
203-
204202
// Send the frame to the encoder
205203
if err := ff.AVCodec_send_frame(e.ctx, (*ff.AVFrame)(frame)); err != nil {
206204
return err
@@ -223,7 +221,7 @@ func (e *Encoder) encode(frame *Frame, fn EncoderPacketFn) error {
223221
e.packet.SetTimeBase(e.stream.TimeBase())
224222

225223
// Pass back to the caller
226-
if err := fn(e.packet, &timebase); errors.Is(err, io.EOF) {
224+
if err := fn((*Packet)(e.packet)); errors.Is(err, io.EOF) {
227225
// End early, return EOF
228226
result = io.EOF
229227
break
@@ -235,9 +233,9 @@ func (e *Encoder) encode(frame *Frame, fn EncoderPacketFn) error {
235233
ff.AVCodec_packet_unref(e.packet)
236234
}
237235

238-
// Flush
236+
// Flush packet
239237
if result == nil {
240-
result = fn(nil, &timebase)
238+
result = fn(nil)
241239
}
242240

243241
// Return success or EOF

pkg/ffmpeg/frame.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ffmpeg
22

33
import (
4+
"encoding/json"
45
"errors"
56

67
// Packages
@@ -78,6 +79,14 @@ func (frame *Frame) Close() error {
7879
return nil
7980
}
8081

82+
///////////////////////////////////////////////////////////////////////////////
83+
// STRINGIFY
84+
85+
func (frame *Frame) String() string {
86+
data, _ := json.MarshalIndent((*ff.AVFrame)(frame), "", " ")
87+
return string(data)
88+
}
89+
8190
///////////////////////////////////////////////////////////////////////////////
8291
// PUBLIC METHODS - FRAME
8392

@@ -101,9 +110,8 @@ func (frame *Frame) CopyPropsFromFrame(other *Frame) error {
101110
return ff.AVUtil_frame_copy_props((*ff.AVFrame)(frame), (*ff.AVFrame)(other))
102111
}
103112

104-
// Return frame type - AUDIO or VIDEO
105-
// other types are not yet identified
106-
// and returned as UNKNOWN
113+
// Return frame type - AUDIO or VIDEO. Other types are not yet
114+
// identified and returned as UNKNOWN
107115
func (frame *Frame) Type() Type {
108116
switch {
109117
case frame.SampleRate() > 0 && frame.SampleFormat() != ff.AV_SAMPLE_FMT_NONE:
@@ -245,6 +253,8 @@ func (frame *Frame) matchesResampleResize(other *Frame) bool {
245253
if frame.Width() != other.Width() || frame.Height() != other.Height() {
246254
return false
247255
}
256+
// We don't need to check the SampleAspectRatio, TimeBase or FrameRate
257+
// for the purposes of resampling or resizing
248258
return true
249259
default:
250260
return false

pkg/ffmpeg/frame.go_old

Lines changed: 0 additions & 251 deletions
This file was deleted.

pkg/ffmpeg/frame_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,30 @@ func Test_frame_001(t *testing.T) {
1717
defer frame.Close()
1818
t.Log(frame)
1919
}
20+
21+
func Test_frame_002(t *testing.T) {
22+
assert := assert.New(t)
23+
24+
frame, err := ffmpeg.NewFrame(ffmpeg.AudioPar("s16", "stereo", 44100))
25+
if !assert.NoError(err) {
26+
t.FailNow()
27+
}
28+
defer frame.Close()
29+
assert.Equal(ffmpeg.AUDIO, frame.Type())
30+
assert.Equal(44100, frame.SampleRate())
31+
t.Log(frame)
32+
}
33+
34+
func Test_frame_003(t *testing.T) {
35+
assert := assert.New(t)
36+
37+
frame, err := ffmpeg.NewFrame(ffmpeg.VideoPar("rgba", "1280x720", 25))
38+
if !assert.NoError(err) {
39+
t.FailNow()
40+
}
41+
defer frame.Close()
42+
assert.Equal(ffmpeg.VIDEO, frame.Type())
43+
assert.Equal(1280, frame.Width())
44+
assert.Equal(720, frame.Height())
45+
t.Log(frame)
46+
}

0 commit comments

Comments
 (0)