|
| 1 | +package ffmpeg |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "io" |
| 8 | + "slices" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + // Packages |
| 13 | + ff "github.com/mutablelogic/go-media/sys/ffmpeg61" |
| 14 | +) |
| 15 | + |
| 16 | +//////////////////////////////////////////////////////////////////////////////// |
| 17 | +// TYPES |
| 18 | + |
| 19 | +// Media reader which reads from a URL, file path or device |
| 20 | +type Reader struct { |
| 21 | + input *ff.AVFormatContext |
| 22 | + avio *ff.AVIOContextEx |
| 23 | +} |
| 24 | + |
| 25 | +type reader_callback struct { |
| 26 | + r io.Reader |
| 27 | +} |
| 28 | + |
| 29 | +// Return parameters if a stream should be decoded and either resampled or |
| 30 | +// resized. Return nil if you want to ignore the stream, or pass back the |
| 31 | +// stream parameters if you want to copy the stream without any changes. |
| 32 | +type DecoderMapFunc func(int, *Par) (*Par, error) |
| 33 | + |
| 34 | +//////////////////////////////////////////////////////////////////////////////// |
| 35 | +// LIFECYCLE |
| 36 | + |
| 37 | +// Open media from a url, file path or device |
| 38 | +func Open(url string, opt ...Opt) (*Reader, error) { |
| 39 | + options := newOpts() |
| 40 | + reader := new(Reader) |
| 41 | + |
| 42 | + // Apply options |
| 43 | + for _, opt := range opt { |
| 44 | + if err := opt(options); err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Get the options |
| 50 | + dict := ff.AVUtil_dict_alloc() |
| 51 | + defer ff.AVUtil_dict_free(dict) |
| 52 | + if len(options.opts) > 0 { |
| 53 | + if err := ff.AVUtil_dict_parse_string(dict, strings.Join(options.opts, " "), "=", " ", 0); err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Open the device or stream |
| 59 | + if ctx, err := ff.AVFormat_open_url(url, options.iformat, dict); err != nil { |
| 60 | + return nil, err |
| 61 | + } else { |
| 62 | + reader.input = ctx |
| 63 | + } |
| 64 | + |
| 65 | + // Find stream information and do rest of the initialization |
| 66 | + return reader.open(options) |
| 67 | +} |
| 68 | + |
| 69 | +// Create a new reader from an io.Reader |
| 70 | +func NewReader(r io.Reader, opt ...Opt) (*Reader, error) { |
| 71 | + options := newOpts() |
| 72 | + reader := new(Reader) |
| 73 | + |
| 74 | + // Apply options |
| 75 | + for _, opt := range opt { |
| 76 | + if err := opt(options); err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Get the options |
| 82 | + dict := ff.AVUtil_dict_alloc() |
| 83 | + defer ff.AVUtil_dict_free(dict) |
| 84 | + if len(options.opts) > 0 { |
| 85 | + if err := ff.AVUtil_dict_parse_string(dict, strings.Join(options.opts, " "), "=", " ", 0); err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Allocate the AVIO context |
| 91 | + reader.avio = ff.AVFormat_avio_alloc_context(bufSize, false, &reader_callback{r}) |
| 92 | + if reader.avio == nil { |
| 93 | + return nil, errors.New("failed to allocate avio context") |
| 94 | + } |
| 95 | + |
| 96 | + // Open the stream |
| 97 | + if ctx, err := ff.AVFormat_open_reader(reader.avio, options.iformat, dict); err != nil { |
| 98 | + ff.AVFormat_avio_context_free(reader.avio) |
| 99 | + return nil, err |
| 100 | + } else { |
| 101 | + reader.input = ctx |
| 102 | + } |
| 103 | + |
| 104 | + // Find stream information and do rest of the initialization |
| 105 | + return reader.open(options) |
| 106 | +} |
| 107 | + |
| 108 | +func (r *Reader) open(_ *opts) (*Reader, error) { |
| 109 | + // Find stream information |
| 110 | + if err := ff.AVFormat_find_stream_info(r.input, nil); err != nil { |
| 111 | + ff.AVFormat_free_context(r.input) |
| 112 | + ff.AVFormat_avio_context_free(r.avio) |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + |
| 116 | + // Return success |
| 117 | + return r, nil |
| 118 | +} |
| 119 | + |
| 120 | +// Close the reader |
| 121 | +func (r *Reader) Close() error { |
| 122 | + var result error |
| 123 | + |
| 124 | + // Free resources |
| 125 | + ff.AVFormat_free_context(r.input) |
| 126 | + if r.avio != nil { |
| 127 | + ff.AVFormat_avio_context_free(r.avio) |
| 128 | + } |
| 129 | + |
| 130 | + // Release resources |
| 131 | + r.input = nil |
| 132 | + r.avio = nil |
| 133 | + |
| 134 | + // Return any errors |
| 135 | + return result |
| 136 | +} |
| 137 | + |
| 138 | +//////////////////////////////////////////////////////////////////////////////// |
| 139 | +// STRINGIFY |
| 140 | + |
| 141 | +// Display the reader as a string |
| 142 | +func (r *Reader) MarshalJSON() ([]byte, error) { |
| 143 | + return json.Marshal(r.input) |
| 144 | +} |
| 145 | + |
| 146 | +// Display the reader as a string |
| 147 | +func (r *Reader) String() string { |
| 148 | + data, _ := json.MarshalIndent(r, "", " ") |
| 149 | + return string(data) |
| 150 | +} |
| 151 | + |
| 152 | +//////////////////////////////////////////////////////////////////////////////// |
| 153 | +// PUBLIC METHODS |
| 154 | + |
| 155 | +// Return the duration of the media stream, returns zero if unknown |
| 156 | +func (r *Reader) Duration() time.Duration { |
| 157 | + duration := r.input.Duration() |
| 158 | + if duration > 0 { |
| 159 | + return time.Duration(duration) * time.Second / time.Duration(ff.AV_TIME_BASE) |
| 160 | + } |
| 161 | + return 0 |
| 162 | +} |
| 163 | + |
| 164 | +// Return the metadata for the media stream, filtering by the specified keys |
| 165 | +// if there are any. Artwork is returned with the "artwork" key. |
| 166 | +func (r *Reader) Metadata(keys ...string) []*Metadata { |
| 167 | + entries := ff.AVUtil_dict_entries(r.input.Metadata()) |
| 168 | + result := make([]*Metadata, 0, len(entries)) |
| 169 | + for _, entry := range entries { |
| 170 | + if len(keys) == 0 || slices.Contains(keys, entry.Key()) { |
| 171 | + result = append(result, NewMetadata(entry.Key(), entry.Value())) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // Obtain any artwork from the streams |
| 176 | + if slices.Contains(keys, MetaArtwork) { |
| 177 | + for _, stream := range r.input.Streams() { |
| 178 | + if packet := stream.AttachedPic(); packet != nil { |
| 179 | + result = append(result, NewMetadata(MetaArtwork, packet.Bytes())) |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + // Return all the metadata |
| 185 | + return result |
| 186 | +} |
| 187 | + |
| 188 | +// TODO Decode the media stream into packets and frames |
| 189 | +func (r *Reader) Decode(ctx context.Context, fn DecoderMapFunc) error { |
| 190 | + return errors.New("not implemented yet") |
| 191 | +} |
| 192 | + |
| 193 | +//////////////////////////////////////////////////////////////////////////////// |
| 194 | +// PRIVATE METHODS |
| 195 | + |
| 196 | +func (r *reader_callback) Reader(buf []byte) int { |
| 197 | + n, err := r.r.Read(buf) |
| 198 | + if err != nil { |
| 199 | + return ff.AVERROR_EOF |
| 200 | + } |
| 201 | + return n |
| 202 | +} |
| 203 | + |
| 204 | +func (r *reader_callback) Seeker(offset int64, whence int) int64 { |
| 205 | + whence = whence & ^ff.AVSEEK_FORCE |
| 206 | + seeker, ok := r.r.(io.ReadSeeker) |
| 207 | + if !ok { |
| 208 | + return -1 |
| 209 | + } |
| 210 | + switch whence { |
| 211 | + case io.SeekStart, io.SeekCurrent, io.SeekEnd: |
| 212 | + n, err := seeker.Seek(offset, whence) |
| 213 | + if err != nil { |
| 214 | + return -1 |
| 215 | + } |
| 216 | + return n |
| 217 | + } |
| 218 | + return -1 |
| 219 | +} |
| 220 | + |
| 221 | +func (r *reader_callback) Writer([]byte) int { |
| 222 | + return ff.AVERROR_EOF |
| 223 | +} |
0 commit comments