Skip to content

Commit d90fc23

Browse files
committed
Updated tests
1 parent 6d2f60d commit d90fc23

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

cmd/ffmpeg/resample_audio/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func main() {
9292
}
9393

9494
// convert to destination format
95-
n, err := ff.SWResample_convert(ctx, dest, dest_nb_samples, src, src_nb_samples)
95+
n, err := ff.SWResample_convert(ctx, dest, src)
9696
if err != nil {
9797
log.Fatal(err)
9898
}

pkg/ffmpeg/writer.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (w *Writer) String() string {
230230
// Return a "stream" for encoding
231231
func (w *Writer) Stream(stream int) *Encoder {
232232
for _, encoder := range w.encoders {
233-
if encoder.stream.Index() == stream {
233+
if encoder.stream.Id() == stream {
234234
return encoder
235235
}
236236
}
@@ -327,21 +327,18 @@ func encode(in EncoderFrameFn, out EncoderPacketFn, encoders map[int]*Encoder) e
327327
var frame *Frame
328328
var err error
329329
if !next_encoder.eof {
330-
frame, err = in(next_stream)
330+
// Get the frame based on the id (rather than index) of the stream
331+
frame, err = in(next_encoder.stream.Id())
331332
if errors.Is(err, io.EOF) {
332333
next_encoder.eof = true
333334
} else if err != nil {
334-
return fmt.Errorf("stream %v: %w", next_stream, err)
335+
return fmt.Errorf("stream %v: %w", next_encoder.stream.Id(), err)
335336
}
336337
}
337-
// If frame not ready, try again
338-
if frame == nil {
339-
return nil
340-
}
341338

342339
// Send a frame for encoding
343340
if err := next_encoder.Encode(frame, out); err != nil {
344-
return fmt.Errorf("stream %v: %w", next_stream, err)
341+
return fmt.Errorf("stream %v: %w", next_encoder.stream.Id(), err)
345342
}
346343

347344
// If eof then delete the encoder
@@ -351,7 +348,11 @@ func encode(in EncoderFrameFn, out EncoderPacketFn, encoders map[int]*Encoder) e
351348
}
352349

353350
// Calculate the next PTS
354-
next_encoder.next_pts = next_encoder.next_pts + next_encoder.nextPts(frame)
351+
if frame != nil {
352+
next_encoder.next_pts = next_encoder.next_pts + next_encoder.nextPts(frame)
353+
}
354+
355+
// Return success
355356
return nil
356357
}
357358

pkg/ffmpeg/writer_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ func Test_writer_002(t *testing.T) {
8686
}
8787
defer audio.Close()
8888

89-
// Write 15 mins of frames
90-
duration := float64(15 * 60)
89+
// Write 15 secs of frames
90+
duration := float64(15)
9191
assert.NoError(writer.Encode(context.Background(), func(stream int) (*ffmpeg.Frame, error) {
9292
frame := audio.Frame()
9393
if frame.Ts() >= duration {
@@ -166,7 +166,7 @@ func Test_writer_004(t *testing.T) {
166166
}
167167
defer w.Close()
168168

169-
// Create a writer with an audio stream
169+
// Create a writer with an audio and video stream
170170
writer, err := ffmpeg.Create(w.Name(),
171171
ffmpeg.OptMetadata(ffmpeg.NewMetadata("title", t.Name())),
172172
ffmpeg.OptStream(1, ffmpeg.VideoPar("yuv420p", "640x480", 30)),
@@ -200,6 +200,7 @@ func Test_writer_004(t *testing.T) {
200200
case 2:
201201
frame = audio.Frame()
202202
}
203+
t.Log("frame = ", stream, frame)
203204
if frame.Ts() >= duration {
204205
t.Log("Frame time is EOF", frame.Ts())
205206
return nil, io.EOF

sys/ffmpeg61/swresample_convert.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,22 @@ func SWResample_get_out_samples(ctx *SWRContext, in_samples int) (int, error) {
6868

6969
// Convert the samples in the input AVFrame and write them to the output AVFrame.
7070
func SWResample_convert_frame(ctx *SWRContext, src, dest *AVFrame) error {
71-
// TODO: This is likely a terrible idea but the only thing I can get to work
72-
// at the moment. Later find out why swr_convert_frame isn't working.
7371
// Ref: https://stackoverflow.com/questions/77502983/libswresample-why-does-swr-init-change-in-ch-layout-order-so-it-no-longer-m
74-
if err := SWResample_config_frame(ctx, src, dest); err != nil {
72+
if err := AVError(C.swr_convert_frame((*C.struct_SwrContext)(ctx), (*C.struct_AVFrame)(dest), (*C.struct_AVFrame)(src))); err == 0 {
73+
return nil
74+
} else if err != AVERROR_INPUT_CHANGED && err != AVERROR_OUTPUT_CHANGED {
75+
return err
76+
} else if err := SWResample_config_frame(ctx, src, dest); err != nil {
7577
return err
7678
}
79+
80+
// Try again
7781
if err := AVError(C.swr_convert_frame((*C.struct_SwrContext)(ctx), (*C.struct_AVFrame)(dest), (*C.struct_AVFrame)(src))); err != 0 {
7882
return err
79-
} else {
80-
return nil
8183
}
84+
85+
// Return success
86+
return nil
8287
}
8388

8489
// Configure or reconfigure the SwrContext using the information provided by the AVFrames.

0 commit comments

Comments
 (0)