| 1 | package ffprobe |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "os/exec" |
| 10 | ) |
| 11 | |
| 12 | var binPath = "ffprobe" |
| 13 | |
| 14 | // SetFFProbeBinPath sets the global path to find and execute the ffprobe program |
| 15 | func SetFFProbeBinPath(newBinPath string) { |
| 16 | binPath = newBinPath |
| 17 | } |
| 18 | |
| 19 | // ProbeURL is used to probe the given media file using ffprobe. The URL can be a local path, a HTTP URL or any other |
| 20 | // protocol supported by ffprobe, see here for a full list: https://ffmpeg.org/ffmpeg-protocols.html |
| 21 | // This function takes a context to allow killing the ffprobe process if it takes too long or in case of shutdown. |
| 22 | // Any additional ffprobe parameter can be supplied as well using extraFFProbeOptions. |
| 23 | func ProbeURL(ctx context.Context, fileURL string, extraFFProbeOptions ...string) (data *ProbeData, err error) { |
| 24 | args := append([]string{ |
| 25 | "-loglevel", "fatal", |
| 26 | "-print_format", "json", |
| 27 | "-show_format", |
| 28 | "-show_streams", |
| 29 | "-show_chapters", |
| 30 | }, extraFFProbeOptions...) |
| 31 | |
| 32 | // Add the file argument |
| 33 | args = append(args, fileURL) |
| 34 | |
| 35 | cmd := exec.CommandContext(ctx, binPath, args...) |
| 36 | cmd.SysProcAttr = procAttributes() |
| 37 | |
| 38 | return runProbe(cmd) |
| 39 | } |
| 40 | |
| 41 | // ProbeReader is used to probe a media file using an io.Reader. The reader is piped to the stdin of the ffprobe command |
| 42 | // and the data is returned. |
| 43 | // This function takes a context to allow killing the ffprobe process if it takes too long or in case of shutdown. |
| 44 | // Any additional ffprobe parameter can be supplied as well using extraFFProbeOptions. |
| 45 | func ProbeReader(ctx context.Context, reader io.Reader, extraFFProbeOptions ...string) (data *ProbeData, err error) { |
| 46 | args := append([]string{ |
| 47 | "-loglevel", "fatal", |
| 48 | "-print_format", "json", |
| 49 | "-show_format", |
| 50 | "-show_streams", |
| 51 | "-show_chapters", |
| 52 | }, extraFFProbeOptions...) |
| 53 | |
| 54 | // Add the file from stdin argument |
| 55 | args = append(args, "-") |
| 56 | |
| 57 | cmd := exec.CommandContext(ctx, binPath, args...) |
| 58 | cmd.Stdin = reader |
| 59 | cmd.SysProcAttr = procAttributes() |
| 60 | |
| 61 | return runProbe(cmd) |
| 62 | } |
| 63 | |
| 64 | // runProbe takes the fully configured ffprobe command and executes it, returning the ffprobe data if everything went fine. |
| 65 | func runProbe(cmd *exec.Cmd) (data *ProbeData, err error) { |
| 66 | var outputBuf bytes.Buffer |
| 67 | var stdErr bytes.Buffer |
| 68 | |
| 69 | cmd.Stdout = &outputBuf |
| 70 | cmd.Stderr = &stdErr |
| 71 | |
| 72 | err = cmd.Run() |
| 73 | if err != nil { |
| 74 | return nil, fmt.Errorf("error running %s [%s] %w", binPath, stdErr.String(), err) |
| 75 | } |
| 76 | |
| 77 | data = &ProbeData{} |
| 78 | err = json.Unmarshal(outputBuf.Bytes(), data) |
| 79 | if err != nil { |
| 80 | return data, fmt.Errorf("error parsing ffprobe output: %w", err) |
| 81 | } |
| 82 | |
| 83 | if data.Format == nil { |
| 84 | return data, fmt.Errorf("no format data found in ffprobe output") |
| 85 | } |
| 86 | |
| 87 | // Populate the old Tags structs for backwards compatibility purposes: |
| 88 | if len(data.Format.TagList) > 0 { |
| 89 | data.Format.Tags = &FormatTags{} |
| 90 | data.Format.Tags.setFrom(data.Format.TagList) |
| 91 | } |
| 92 | for _, str := range data.Streams { |
| 93 | str.Tags.setFrom(str.TagList) |
| 94 | } |
| 95 | |
| 96 | return data, nil |
| 97 | } |