Skip to content

Commit 6f65340

Browse files
committed
Updated examples
1 parent 7d067c9 commit 6f65340

File tree

4 files changed

+75
-44
lines changed

4 files changed

+75
-44
lines changed

cmd/examples/decode/main.go

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,47 @@
11
package main
22

33
import (
4-
"flag"
4+
"context"
55
"log"
66
"os"
77

8-
// Packages
9-
ffmpeg "github.com/mutablelogic/go-media"
10-
)
11-
12-
var (
13-
in = flag.String("in", "", "input file to decode")
14-
audio_stream = flag.Int("audio", -1, "audio stream to decode")
15-
video_stream = flag.Int("video", -1, "video stream to decode")
8+
media "github.com/mutablelogic/go-media"
169
)
1710

1811
func main() {
19-
flag.Parse()
20-
21-
// Check input file - read it
22-
if *in == "" {
23-
log.Fatal("-in flag must be specified")
24-
}
25-
r, err := os.Open(*in)
26-
if err != nil {
27-
log.Fatal(err)
28-
}
29-
defer r.Close()
12+
manager := media.NewManager()
3013

31-
input, err := ffmpeg.NewReader(r, "")
14+
// Open a media file for reading. The format of the file is guessed.
15+
// Alteratively, you can pass a format as the second argument. Further optional
16+
// arguments can be used to set the format options.
17+
file, err := manager.Open(os.Args[1], nil)
3218
if err != nil {
3319
log.Fatal(err)
3420
}
35-
defer input.Close()
36-
37-
// Create a decoder for audio
38-
audio, err := input.NewDecoder(ffmpeg.AUDIO, *audio_stream)
21+
defer file.Close()
22+
23+
// Choose which streams to demultiplex - pass the stream parameters
24+
// to the decoder. If you don't want to resample or reformat the streams,
25+
// then you can pass nil as the function and all streams will be demultiplexed.
26+
decoder, err := file.Decoder(func(stream media.Stream) (media.Parameters, error) {
27+
// Copy streams, don't resample or resize
28+
return stream.Parameters(), nil
29+
})
3930
if err != nil {
4031
log.Fatal(err)
41-
} else if err := audio.ResampleS16Mono(22000); err != nil {
42-
log.Fatal(err)
4332
}
4433

45-
// Create a decoder for video
46-
video, err := input.NewDecoder(ffmpeg.VIDEO, *video_stream)
47-
if err != nil {
48-
log.Fatal(err)
49-
} else if err := video.Rescale(1024, 720); err != nil {
50-
log.Fatal(err)
51-
}
34+
// Demuliplex the stream and receive the frames of audio and video.
35+
if err := decoder.Decode(context.Background(), func(frame media.Frame) error {
36+
// Each packet is specific to a stream. It can be processed here
37+
// to receive audio or video frames, then resize or resample them,
38+
// for example. Alternatively, you can pass the packet to an encoder
39+
// to remultiplex the streams without processing them.
40+
log.Print(frame)
5241

53-
// Demux and decode the audio and video
54-
n := 0
55-
if err := input.Demux(input.Decode(func(frame ffmpeg.Frame) error {
56-
log.Print("frame: ", n, "=>", frame)
57-
n++
42+
// Return io.EOF to stop processing, nil to continue
5843
return nil
59-
})); err != nil {
60-
44+
}); err != nil {
6145
log.Fatal(err)
6246
}
6347
}

cmd/examples/demux/main.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
"os"
7+
8+
media "github.com/mutablelogic/go-media"
9+
)
10+
11+
func main() {
12+
manager := media.NewManager()
13+
14+
// Open a media file for reading. The format of the file is guessed.
15+
// Alteratively, you can pass a format as the second argument. Further optional
16+
// arguments can be used to set the format options.
17+
file, err := manager.Open(os.Args[1], nil)
18+
if err != nil {
19+
log.Fatal(err)
20+
}
21+
defer file.Close()
22+
23+
// Choose which streams to demultiplex - pass the stream parameters
24+
// to the decoder. If you don't want to resample or reformat the streams,
25+
// then you can pass nil as the function and all streams will be demultiplexed.
26+
decoder, err := file.Decoder(func(stream media.Stream) (media.Parameters, error) {
27+
return stream.Parameters(), nil
28+
})
29+
if err != nil {
30+
log.Fatal(err)
31+
}
32+
33+
// Demuliplex the stream and receive the packets from the stream
34+
if err := decoder.Demux(context.Background(), func(packet media.Packet) error {
35+
// Each packet is specific to a stream. It can be processed here
36+
// to decode audio or video frames, then resize or resample them,
37+
// for example. Alternatively, you can pass the packet to an encoder
38+
// to remultiplex the streams without processing them.
39+
// You may get 'nil' packets when the stream is flushed.
40+
log.Print(packet)
41+
42+
// Return io.EOF to stop processing, nil to continue
43+
return nil
44+
}); err != nil {
45+
log.Fatal(err)
46+
}
47+
}

decoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,13 @@ func equalsStream(dest Parameters, src *ff.AVCodecParameters) (bool, error) {
365365
switch src.CodecType() {
366366
case ff.AVMEDIA_TYPE_AUDIO:
367367
if !dest.Type().Is(AUDIO) {
368-
return false, fmt.Errorf("source is audio, but destination is %v", dest.Type())
368+
return false, fmt.Errorf("source is AUDIO, but destination is %v", dest.Type())
369369
} else {
370370
return equalsAudioPar(dest, src), nil
371371
}
372372
case ff.AVMEDIA_TYPE_VIDEO:
373373
if !dest.Type().Is(VIDEO) {
374-
return false, fmt.Errorf("source is video, but destination are %v", dest.Type())
374+
return false, fmt.Errorf("source is VIDEO, but destination is %v", dest.Type())
375375
} else {
376376
return equalsVideoPar(dest, src), nil
377377
}

mediatype.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (v MediaType) MarshalJSON() ([]byte, error) {
4040

4141
func (v MediaType) String() string {
4242
if v == NONE {
43-
return v.String()
43+
return v.FlagString()
4444
}
4545
str := ""
4646
for f := MIN; f <= MAX; f <<= 1 {

0 commit comments

Comments
 (0)