From aad936c7cc397bf9513673ab30f580c0c5c6b6ec Mon Sep 17 00:00:00 2001 From: Wenhui Shen Date: Thu, 10 Jun 2021 02:30:08 +0800 Subject: [PATCH 01/12] update --- README.md | 4 ++-- ffmpeg/config.go | 5 +++++ ffmpeg/ffmpeg.go | 17 +++++++++++++---- ffmpeg/metadata.go | 2 +- go.mod | 2 +- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 651b613..3693e9b 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ ## Download from Github ```shell -go get github.com/floostack/transcoder +go get github.com/admpub/transcoder ``` ## Example @@ -45,7 +45,7 @@ package main import ( "log" - ffmpeg "github.com/floostack/transcoder/ffmpeg" + ffmpeg "github.com/admpub/transcoder/ffmpeg" ) func main() { diff --git a/ffmpeg/config.go b/ffmpeg/config.go index 05eb7ff..10cfa1c 100644 --- a/ffmpeg/config.go +++ b/ffmpeg/config.go @@ -1,9 +1,14 @@ package ffmpeg +import "github.com/admpub/transcoder" + // Config ... type Config struct { FfmpegBinPath string FfprobeBinPath string ProgressEnabled bool Verbose bool + Env []string + Dir string + OnMetadata func(transcoder.Metadata) error } diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index d67b2e9..64dc61f 100644 --- a/ffmpeg/ffmpeg.go +++ b/ffmpeg/ffmpeg.go @@ -13,8 +13,8 @@ import ( "strconv" "strings" - "github.com/floostack/transcoder" - "github.com/floostack/transcoder/utils" + "github.com/admpub/transcoder" + "github.com/admpub/transcoder/utils" ) // Transcoder ... @@ -50,10 +50,15 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, } // Get file metadata - _, err := t.GetMetadata() + metadata, err := t.GetMetadata() if err != nil { return nil, err } + if t.config.OnMetadata != nil { + if err := t.config.OnMetadata(metadata); err != nil { + return nil, err + } + } // Append input file and standard options args := append([]string{"-i", t.input}, opts.GetStrArguments()...) @@ -83,6 +88,8 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, // Initialize command cmd := exec.Command(t.config.FfmpegBinPath, args...) + cmd.Env = append(t.config.Env, os.Environ()...) + cmd.Dir = t.config.Dir // If progresss enabled, get stderr pipe and start progress process if t.config.ProgressEnabled && !t.config.Verbose { @@ -192,7 +199,7 @@ func (t *Transcoder) validate() error { } // GetMetadata Returns metadata for the specified input file -func (t *Transcoder) GetMetadata() ( transcoder.Metadata, error) { +func (t *Transcoder) GetMetadata() (transcoder.Metadata, error) { if t.config.FfprobeBinPath != "" { var outb, errb bytes.Buffer @@ -208,6 +215,8 @@ func (t *Transcoder) GetMetadata() ( transcoder.Metadata, error) { cmd := exec.Command(t.config.FfprobeBinPath, args...) cmd.Stdout = &outb cmd.Stderr = &errb + cmd.Env = append(t.config.Env, os.Environ()...) + cmd.Dir = t.config.Dir err := cmd.Run() if err != nil { diff --git a/ffmpeg/metadata.go b/ffmpeg/metadata.go index 4e81910..58449b5 100644 --- a/ffmpeg/metadata.go +++ b/ffmpeg/metadata.go @@ -1,6 +1,6 @@ package ffmpeg -import "github.com/floostack/transcoder" +import "github.com/admpub/transcoder" // Metadata ... type Metadata struct { diff --git a/go.mod b/go.mod index 4c9ade4..1e14137 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/floostack/transcoder +module github.com/admpub/transcoder go 1.13 From 33aa4e6a66e4407e273dab937e56efcf29163c76 Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Mon, 5 Jul 2021 00:28:22 +0800 Subject: [PATCH 02/12] update --- ffmpeg/ffmpeg.go | 42 +++++++++++++++++++++++------------------- transcoder.go | 4 ++-- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index 64dc61f..40c1db8 100644 --- a/ffmpeg/ffmpeg.go +++ b/ffmpeg/ffmpeg.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "io" + "log" "os" "os/exec" "regexp" @@ -24,10 +25,10 @@ type Transcoder struct { output []string options [][]string metadata transcoder.Metadata - inputPipeReader *io.ReadCloser - outputPipeReader *io.ReadCloser - inputPipeWriter *io.WriteCloser - outputPipeWriter *io.WriteCloser + inputPipeReader io.ReadCloser + outputPipeReader io.ReadCloser + inputPipeWriter io.WriteCloser + outputPipeWriter io.WriteCloser } // New ... @@ -40,8 +41,6 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, var stderrIn io.ReadCloser - out := make(chan transcoder.Progress) - defer t.closePipes() // Validates config @@ -95,7 +94,7 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, if t.config.ProgressEnabled && !t.config.Verbose { stderrIn, err = cmd.StderrPipe() if err != nil { - return nil, fmt.Errorf("Failed getting transcoding progress (%s) with args (%s) with error %s", t.config.FfmpegBinPath, args, err) + return nil, fmt.Errorf("failed getting transcoding progress (%s) with args (%s) with error %w", t.config.FfmpegBinPath, args, err) } } @@ -106,9 +105,10 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, // Start process err = cmd.Start() if err != nil { - return nil, fmt.Errorf("Failed starting transcoding (%s) with args (%s) with error %s", t.config.FfmpegBinPath, args, err) + return nil, fmt.Errorf("failed starting transcoding (%s) with args (%s) with error %w", t.config.FfmpegBinPath, args, err) } + out := make(chan transcoder.Progress) if t.config.ProgressEnabled && !t.config.Verbose { go func() { t.progress(stderrIn, out) @@ -117,12 +117,18 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, go func() { defer close(out) err = cmd.Wait() + if err != nil { + log.Printf("failed to transcoding (%s) with args (%s) with error %v", t.config.FfmpegBinPath, args, err) + } }() } else { err = cmd.Wait() + if err != nil { + return nil, fmt.Errorf("failed to transcoding (%s) with args (%s) with error %w", t.config.FfmpegBinPath, args, err) + } } - return out, nil + return out, err } // Input ... @@ -138,8 +144,8 @@ func (t *Transcoder) Output(arg string) transcoder.Transcoder { } // InputPipe ... -func (t *Transcoder) InputPipe(w *io.WriteCloser, r *io.ReadCloser) transcoder.Transcoder { - if &t.input == nil { +func (t *Transcoder) InputPipe(w io.WriteCloser, r io.ReadCloser) transcoder.Transcoder { + if len(t.input) == 0 { t.inputPipeWriter = w t.inputPipeReader = r } @@ -147,8 +153,8 @@ func (t *Transcoder) InputPipe(w *io.WriteCloser, r *io.ReadCloser) transcoder.T } // OutputPipe ... -func (t *Transcoder) OutputPipe(w *io.WriteCloser, r *io.ReadCloser) transcoder.Transcoder { - if &t.output == nil { +func (t *Transcoder) OutputPipe(w io.WriteCloser, r io.ReadCloser) transcoder.Transcoder { + if len(t.output) == 0 { t.outputPipeWriter = w t.outputPipeReader = r } @@ -201,7 +207,7 @@ func (t *Transcoder) validate() error { // GetMetadata Returns metadata for the specified input file func (t *Transcoder) GetMetadata() (transcoder.Metadata, error) { - if t.config.FfprobeBinPath != "" { + if len(t.config.FfprobeBinPath) > 0 { var outb, errb bytes.Buffer input := t.input @@ -225,7 +231,7 @@ func (t *Transcoder) GetMetadata() (transcoder.Metadata, error) { var metadata Metadata - if err = json.Unmarshal([]byte(outb.String()), &metadata); err != nil { + if err = json.Unmarshal(outb.Bytes(), &metadata); err != nil { return nil, err } @@ -326,12 +332,10 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress // closePipes Closes pipes if opened func (t *Transcoder) closePipes() { if t.inputPipeReader != nil { - ipr := *t.inputPipeReader - ipr.Close() + t.inputPipeReader.Close() } if t.outputPipeWriter != nil { - opr := *t.outputPipeWriter - opr.Close() + t.outputPipeWriter.Close() } } diff --git a/transcoder.go b/transcoder.go index 9e58206..ef32bcd 100644 --- a/transcoder.go +++ b/transcoder.go @@ -8,9 +8,9 @@ import ( type Transcoder interface { Start(opts Options) (<-chan Progress, error) Input(i string) Transcoder - InputPipe(w *io.WriteCloser, r *io.ReadCloser) Transcoder + InputPipe(w io.WriteCloser, r io.ReadCloser) Transcoder Output(o string) Transcoder - OutputPipe(w *io.WriteCloser, r *io.ReadCloser) Transcoder + OutputPipe(w io.WriteCloser, r io.ReadCloser) Transcoder WithOptions(opts Options) Transcoder WithAdditionalOptions(opts Options) Transcoder GetMetadata() (Metadata, error) From 24e6bb57d9fbd00a6459890cff7c5793a02c18c1 Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Mon, 5 Jul 2021 21:12:01 +0800 Subject: [PATCH 03/12] update --- ffmpeg/ffmpeg.go | 14 +++++++++----- ffmpeg/options.go | 13 +++++++++---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index 40c1db8..5a401c8 100644 --- a/ffmpeg/ffmpeg.go +++ b/ffmpeg/ffmpeg.go @@ -23,7 +23,7 @@ type Transcoder struct { config *Config input string output []string - options [][]string + options []transcoder.Options metadata transcoder.Metadata inputPipeReader io.ReadCloser outputPipeReader io.ReadCloser @@ -68,16 +68,20 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, // Just append the 1 output file we've got args = append(args, t.output[0]) } else { + arguments := make([][]string, len(t.options)) + for i, o := range t.options { + arguments[i] = o.GetStrArguments() + } for index, out := range t.output { // Get executable flags // If we are at the last output file but still have several options, append them all at once if index == outputLength-1 && outputLength < optionsLength { for i := index; i < len(t.options); i++ { - args = append(args, t.options[i]...) + args = append(args, arguments[i]...) } // Otherwise just append the current options } else { - args = append(args, t.options[index]...) + args = append(args, arguments[index]...) } // Append output flag @@ -163,13 +167,13 @@ func (t *Transcoder) OutputPipe(w io.WriteCloser, r io.ReadCloser) transcoder.Tr // WithOptions Sets the options object func (t *Transcoder) WithOptions(opts transcoder.Options) transcoder.Transcoder { - t.options = [][]string{opts.GetStrArguments()} + t.options = []transcoder.Options{opts} return t } // WithAdditionalOptions Appends an additional options object func (t *Transcoder) WithAdditionalOptions(opts transcoder.Options) transcoder.Transcoder { - t.options = append(t.options, opts.GetStrArguments()) + t.options = append(t.options, opts) return t } diff --git a/ffmpeg/options.go b/ffmpeg/options.go index 323d4b3..15d5f2d 100644 --- a/ffmpeg/options.go +++ b/ffmpeg/options.go @@ -77,34 +77,39 @@ func (opts Options) GetStrArguments() []string { for i := 0; i < f.NumField(); i++ { flag := f.Field(i).Tag.Get("flag") - value := v.Field(i).Interface() + rv := v.Field(i) + value := rv.Interface() - if !v.Field(i).IsNil() { + if !rv.IsNil() { if _, ok := value.(*bool); ok { values = append(values, flag) + continue } if vs, ok := value.(*string); ok { values = append(values, flag, *vs) + continue } if va, ok := value.([]string); ok { - for i := 0; i < len(va); i++ { item := va[i] values = append(values, flag, item) } + continue } if vm, ok := value.(map[string]interface{}); ok { for k, v := range vm { values = append(values, k, fmt.Sprintf("%v", v)) } + continue } - + if vi, ok := value.(*int); ok { values = append(values, flag, fmt.Sprintf("%d", *vi)) + continue } } From 2832aff0900af4049fd9eac4cf3a94ac05446573 Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Mon, 10 Jan 2022 14:40:51 +0800 Subject: [PATCH 04/12] update --- transcoder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transcoder.go b/transcoder.go index 412b737..03ccaa7 100644 --- a/transcoder.go +++ b/transcoder.go @@ -14,6 +14,6 @@ type Transcoder interface { OutputPipe(w io.WriteCloser, r io.ReadCloser) Transcoder WithOptions(opts Options) Transcoder WithAdditionalOptions(opts Options) Transcoder - WithContext(ctx *context.Context) Transcoder + WithContext(ctx context.Context) Transcoder GetMetadata() (Metadata, error) } From 1e5198117688a454de9a2ca04030c6889bc29456 Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Fri, 11 Feb 2022 20:34:41 +0800 Subject: [PATCH 05/12] update --- ffmpeg/ffmpeg.go | 4 +++- ffmpeg/progress.go | 6 ++++++ progress.go | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index 9711e42..aa5cd97 100644 --- a/ffmpeg/ffmpeg.go +++ b/ffmpeg/ffmpeg.go @@ -132,7 +132,9 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, defer close(out) err = cmd.Wait() if err != nil { - log.Printf("failed to transcoding (%s) with args (%s) with error %v", t.config.FfmpegBinPath, args, err) + err = fmt.Errorf("failed to transcoding (%s) with args (%s) with error %w", t.config.FfmpegBinPath, args, err) + log.Println(err) + out <- &Progress{Error: err} } }() } else { diff --git a/ffmpeg/progress.go b/ffmpeg/progress.go index 62594c6..e305a5c 100644 --- a/ffmpeg/progress.go +++ b/ffmpeg/progress.go @@ -7,6 +7,7 @@ type Progress struct { CurrentBitrate string Progress float64 Speed string + Error error } // GetFramesProcessed ... @@ -33,3 +34,8 @@ func (p Progress) GetProgress() float64 { func (p Progress) GetSpeed() string { return p.Speed } + +// GetError ... +func (p Progress) GetError() error { + return p.Error +} diff --git a/progress.go b/progress.go index 4b7b060..8537680 100644 --- a/progress.go +++ b/progress.go @@ -7,4 +7,5 @@ type Progress interface { GetCurrentBitrate() string GetProgress() float64 GetSpeed() string + GetError() error } From a354e6a22f03c35bda4cd9ca2c312c9ea84a5ac6 Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Sat, 12 Feb 2022 19:55:19 +0800 Subject: [PATCH 06/12] update --- ffmpeg/ffmpeg.go | 36 +++++++++++++++++++++++++++++++++++- ffmpeg/metadata.go | 5 +++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index aa5cd97..725b2b5 100644 --- a/ffmpeg/ffmpeg.go +++ b/ffmpeg/ffmpeg.go @@ -263,7 +263,41 @@ func (t *Transcoder) GetMetadata() (transcoder.Metadata, error) { if err = json.Unmarshal(outb.Bytes(), &metadata); err != nil { return nil, err } - + metadata.Infos = map[string]string{} + reader := bufio.NewReader(&errb) + var prefix string + var hasMetadata bool + for { + byteLine, isPrefix, err := reader.ReadLine() + if err != nil && err != io.EOF { + return metadata, err + } + line := string(byteLine) + if isPrefix { + prefix += line + continue + } + line = prefix + line + line = strings.TrimLeft(line, ` `) + if !hasMetadata { + if strings.HasPrefix(line, `Metadata:`) { + hasMetadata = true + } + } else { + parts := strings.SplitN(line, `:`, 2) + if len(parts) == 2 { + parts[0] = strings.TrimRight(parts[0], ` `) + if len(parts[0]) > 0 && parts[0] != `Metadata:` { + parts[1] = strings.TrimSpace(parts[1]) + metadata.Infos[parts[0]] = parts[1] + } + } + } + prefix = "" + if err == io.EOF { + break + } + } t.metadata = metadata return metadata, nil diff --git a/ffmpeg/metadata.go b/ffmpeg/metadata.go index 58449b5..6ebaf6d 100644 --- a/ffmpeg/metadata.go +++ b/ffmpeg/metadata.go @@ -4,8 +4,9 @@ import "github.com/admpub/transcoder" // Metadata ... type Metadata struct { - Format Format `json:"format"` - Streams []Streams `json:"streams"` + Format Format `json:"format"` + Streams []Streams `json:"streams"` + Infos map[string]string `json:"infos"` } // Format ... From 6ea55de95ec33fc5eb4c1f7e15154dbeb963e1e5 Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Sat, 12 Feb 2022 20:10:06 +0800 Subject: [PATCH 07/12] update --- ffmpeg/ffmpeg.go | 45 ++++++----------------------- ffmpeg/metadata.go | 71 ++++++++++++++++++++++++++-------------------- metadata.go | 2 ++ 3 files changed, 51 insertions(+), 67 deletions(-) diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index 725b2b5..395f315 100644 --- a/ffmpeg/ffmpeg.go +++ b/ffmpeg/ffmpeg.go @@ -240,7 +240,14 @@ func (t *Transcoder) GetMetadata() (transcoder.Metadata, error) { input = "pipe:" } - args := []string{"-i", input, "-print_format", "json", "-show_format", "-show_streams", "-show_error"} + args := []string{ + "-i", input, + "-print_format", "json", + "-show_entries", "stream=:stream_tags=rotate", + "-show_format", + "-show_streams", + "-show_error", + } var cmd *exec.Cmd if t.commandContext == nil { @@ -263,41 +270,7 @@ func (t *Transcoder) GetMetadata() (transcoder.Metadata, error) { if err = json.Unmarshal(outb.Bytes(), &metadata); err != nil { return nil, err } - metadata.Infos = map[string]string{} - reader := bufio.NewReader(&errb) - var prefix string - var hasMetadata bool - for { - byteLine, isPrefix, err := reader.ReadLine() - if err != nil && err != io.EOF { - return metadata, err - } - line := string(byteLine) - if isPrefix { - prefix += line - continue - } - line = prefix + line - line = strings.TrimLeft(line, ` `) - if !hasMetadata { - if strings.HasPrefix(line, `Metadata:`) { - hasMetadata = true - } - } else { - parts := strings.SplitN(line, `:`, 2) - if len(parts) == 2 { - parts[0] = strings.TrimRight(parts[0], ` `) - if len(parts[0]) > 0 && parts[0] != `Metadata:` { - parts[1] = strings.TrimSpace(parts[1]) - metadata.Infos[parts[0]] = parts[1] - } - } - } - prefix = "" - if err == io.EOF { - break - } - } + t.metadata = metadata return metadata, nil diff --git a/ffmpeg/metadata.go b/ffmpeg/metadata.go index 6ebaf6d..abbbd60 100644 --- a/ffmpeg/metadata.go +++ b/ffmpeg/metadata.go @@ -4,9 +4,8 @@ import "github.com/admpub/transcoder" // Metadata ... type Metadata struct { - Format Format `json:"format"` - Streams []Streams `json:"streams"` - Infos map[string]string `json:"infos"` + Format Format `json:"format"` + Streams []Streams `json:"streams"` } // Format ... @@ -26,34 +25,36 @@ type Format struct { // Streams ... type Streams struct { Index int - ID string `json:"id"` - CodecName string `json:"codec_name"` - CodecLongName string `json:"codec_long_name"` - Profile string `json:"profile"` - CodecType string `json:"codec_type"` - CodecTimeBase string `json:"codec_time_base"` - CodecTagString string `json:"codec_tag_string"` - CodecTag string `json:"codec_tag"` - Width int `json:"width"` - Height int `json:"height"` - CodedWidth int `json:"coded_width"` - CodedHeight int `json:"coded_height"` - HasBFrames int `json:"has_b_frames"` - SampleAspectRatio string `json:"sample_aspect_ratio"` - DisplayAspectRatio string `json:"display_aspect_ratio"` - PixFmt string `json:"pix_fmt"` - Level int `json:"level"` - ChromaLocation string `json:"chroma_location"` - Refs int `json:"refs"` - QuarterSample string `json:"quarter_sample"` - DivxPacked string `json:"divx_packed"` - RFrameRrate string `json:"r_frame_rate"` - AvgFrameRate string `json:"avg_frame_rate"` - TimeBase string `json:"time_base"` - DurationTs int `json:"duration_ts"` - Duration string `json:"duration"` - Disposition Disposition `json:"disposition"` - BitRate string `json:"bit_rate"` + ID string `json:"id"` + CodecName string `json:"codec_name"` + CodecLongName string `json:"codec_long_name"` + Profile string `json:"profile"` + CodecType string `json:"codec_type"` + CodecTimeBase string `json:"codec_time_base"` + CodecTagString string `json:"codec_tag_string"` + CodecTag string `json:"codec_tag"` + Width int `json:"width"` + Height int `json:"height"` + CodedWidth int `json:"coded_width"` + CodedHeight int `json:"coded_height"` + HasBFrames int `json:"has_b_frames"` + SampleAspectRatio string `json:"sample_aspect_ratio"` + DisplayAspectRatio string `json:"display_aspect_ratio"` + PixFmt string `json:"pix_fmt"` + Level int `json:"level"` + ChromaLocation string `json:"chroma_location"` + Refs int `json:"refs"` + QuarterSample string `json:"quarter_sample"` + DivxPacked string `json:"divx_packed"` + RFrameRrate string `json:"r_frame_rate"` + AvgFrameRate string `json:"avg_frame_rate"` + TimeBase string `json:"time_base"` + DurationTs int `json:"duration_ts"` + Duration string `json:"duration"` + Disposition Disposition `json:"disposition"` + BitRate string `json:"bit_rate"` + Tags map[string]string `json:"tags"` + SideDataList []map[string]string `json:"side_data_list"` } // Tags ... @@ -288,6 +289,14 @@ func (s Streams) GetBitRate() string { return s.BitRate } +func (s Streams) GetTags() map[string]string { + return s.Tags +} + +func (s Streams) GetSideDataList() []map[string]string { + return s.SideDataList +} + //GetDefault ... func (d Disposition) GetDefault() int { return d.Default diff --git a/metadata.go b/metadata.go index d03241d..3956215 100644 --- a/metadata.go +++ b/metadata.go @@ -51,6 +51,8 @@ type Streams interface { GetDuration() string GetDisposition() Disposition GetBitRate() string + GetTags() map[string]string + GetSideDataList() []map[string]string } // Tags ... From e824d05907328a495cc951b9337d9d008eec920f Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Sat, 12 Feb 2022 20:14:21 +0800 Subject: [PATCH 08/12] update --- ffmpeg/metadata.go | 62 +++++++++++++++++++++++----------------------- metadata.go | 2 +- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/ffmpeg/metadata.go b/ffmpeg/metadata.go index abbbd60..000c82d 100644 --- a/ffmpeg/metadata.go +++ b/ffmpeg/metadata.go @@ -25,36 +25,36 @@ type Format struct { // Streams ... type Streams struct { Index int - ID string `json:"id"` - CodecName string `json:"codec_name"` - CodecLongName string `json:"codec_long_name"` - Profile string `json:"profile"` - CodecType string `json:"codec_type"` - CodecTimeBase string `json:"codec_time_base"` - CodecTagString string `json:"codec_tag_string"` - CodecTag string `json:"codec_tag"` - Width int `json:"width"` - Height int `json:"height"` - CodedWidth int `json:"coded_width"` - CodedHeight int `json:"coded_height"` - HasBFrames int `json:"has_b_frames"` - SampleAspectRatio string `json:"sample_aspect_ratio"` - DisplayAspectRatio string `json:"display_aspect_ratio"` - PixFmt string `json:"pix_fmt"` - Level int `json:"level"` - ChromaLocation string `json:"chroma_location"` - Refs int `json:"refs"` - QuarterSample string `json:"quarter_sample"` - DivxPacked string `json:"divx_packed"` - RFrameRrate string `json:"r_frame_rate"` - AvgFrameRate string `json:"avg_frame_rate"` - TimeBase string `json:"time_base"` - DurationTs int `json:"duration_ts"` - Duration string `json:"duration"` - Disposition Disposition `json:"disposition"` - BitRate string `json:"bit_rate"` - Tags map[string]string `json:"tags"` - SideDataList []map[string]string `json:"side_data_list"` + ID string `json:"id"` + CodecName string `json:"codec_name"` + CodecLongName string `json:"codec_long_name"` + Profile string `json:"profile"` + CodecType string `json:"codec_type"` + CodecTimeBase string `json:"codec_time_base"` + CodecTagString string `json:"codec_tag_string"` + CodecTag string `json:"codec_tag"` + Width int `json:"width"` + Height int `json:"height"` + CodedWidth int `json:"coded_width"` + CodedHeight int `json:"coded_height"` + HasBFrames int `json:"has_b_frames"` + SampleAspectRatio string `json:"sample_aspect_ratio"` + DisplayAspectRatio string `json:"display_aspect_ratio"` + PixFmt string `json:"pix_fmt"` + Level int `json:"level"` + ChromaLocation string `json:"chroma_location"` + Refs int `json:"refs"` + QuarterSample string `json:"quarter_sample"` + DivxPacked string `json:"divx_packed"` + RFrameRrate string `json:"r_frame_rate"` + AvgFrameRate string `json:"avg_frame_rate"` + TimeBase string `json:"time_base"` + DurationTs int `json:"duration_ts"` + Duration string `json:"duration"` + Disposition Disposition `json:"disposition"` + BitRate string `json:"bit_rate"` + Tags map[string]string `json:"tags"` + SideDataList []map[string]interface{} `json:"side_data_list"` } // Tags ... @@ -293,7 +293,7 @@ func (s Streams) GetTags() map[string]string { return s.Tags } -func (s Streams) GetSideDataList() []map[string]string { +func (s Streams) GetSideDataList() []map[string]interface{} { return s.SideDataList } diff --git a/metadata.go b/metadata.go index 3956215..89bd992 100644 --- a/metadata.go +++ b/metadata.go @@ -52,7 +52,7 @@ type Streams interface { GetDisposition() Disposition GetBitRate() string GetTags() map[string]string - GetSideDataList() []map[string]string + GetSideDataList() []map[string]interface{} } // Tags ... From 4392c5ac152808f03572f7ec08fe89768e776aef Mon Sep 17 00:00:00 2001 From: Wenhui Shen Date: Sun, 13 Feb 2022 11:16:08 +0800 Subject: [PATCH 09/12] update --- ffmpeg/ffmpeg.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index 395f315..60d5013 100644 --- a/ffmpeg/ffmpeg.go +++ b/ffmpeg/ffmpeg.go @@ -124,8 +124,11 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, out := make(chan transcoder.Progress) if t.config.ProgressEnabled && !t.config.Verbose { + done := make(chan struct{}) go func() { t.progress(stderrIn, out) + done <- struct{}{} + close(done) }() go func() { @@ -136,6 +139,7 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, log.Println(err) out <- &Progress{Error: err} } + <-done }() } else { err = cmd.Wait() From ef1aff3d2b6700d082c2bff0f3aae3db6e8fcaaf Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Sun, 13 Feb 2022 12:48:04 +0800 Subject: [PATCH 10/12] update --- ffmpeg/ffmpeg.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index 60d5013..6b6be7e 100644 --- a/ffmpeg/ffmpeg.go +++ b/ffmpeg/ffmpeg.go @@ -283,6 +283,8 @@ func (t *Transcoder) GetMetadata() (transcoder.Metadata, error) { return nil, errors.New("ffprobe binary not found") } +var reEQ = regexp.MustCompile(`=\s+`) + // progress sends through given channel the transcoding status func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress) { @@ -318,11 +320,8 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress line := scanner.Text() if strings.Contains(line, "time=") && strings.Contains(line, "bitrate=") { - var re = regexp.MustCompile(`=\s+`) - st := re.ReplaceAllString(line, `=`) - + st := reEQ.ReplaceAllString(line, `=`) f := strings.Fields(st) - var framesProcessed string var currentTime string var currentBitrate string From c9ef7977e597f30f860fbc17a2110ca1aadcbe3e Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Wed, 16 Feb 2022 16:42:58 +0800 Subject: [PATCH 11/12] update --- ffmpeg/ffmpeg.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index 6b6be7e..9e538d5 100644 --- a/ffmpeg/ffmpeg.go +++ b/ffmpeg/ffmpeg.go @@ -135,7 +135,8 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, defer close(out) err = cmd.Wait() if err != nil { - err = fmt.Errorf("failed to transcoding (%s) with args (%s) with error %w", t.config.FfmpegBinPath, args, err) + b, _ := io.ReadAll(io.LimitReader(stderrIn, 100)) + err = fmt.Errorf("failed to transcoding (%s) with args (%s) with error %w: %s", t.config.FfmpegBinPath, args, err, string(b)) log.Println(err) out <- &Progress{Error: err} } @@ -144,7 +145,8 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, } else { err = cmd.Wait() if err != nil { - return nil, fmt.Errorf("failed to transcoding (%s) with args (%s) with error %w", t.config.FfmpegBinPath, args, err) + b, _ := io.ReadAll(io.LimitReader(stderrIn, 100)) + return nil, fmt.Errorf("failed to transcoding (%s) with args (%s) with error %w: %s", t.config.FfmpegBinPath, args, err, string(b)) } } From cbe703f34edab9960df11e3fa847cb5b3521fdd7 Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Wed, 16 Feb 2022 17:03:42 +0800 Subject: [PATCH 12/12] update --- ffmpeg/ffmpeg.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index 9e538d5..95fff24 100644 --- a/ffmpeg/ffmpeg.go +++ b/ffmpeg/ffmpeg.go @@ -135,8 +135,7 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, defer close(out) err = cmd.Wait() if err != nil { - b, _ := io.ReadAll(io.LimitReader(stderrIn, 100)) - err = fmt.Errorf("failed to transcoding (%s) with args (%s) with error %w: %s", t.config.FfmpegBinPath, args, err, string(b)) + err = fmt.Errorf("failed to transcoding (%s) with args (%s) with error %w", t.config.FfmpegBinPath, args, err) log.Println(err) out <- &Progress{Error: err} } @@ -145,8 +144,7 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, } else { err = cmd.Wait() if err != nil { - b, _ := io.ReadAll(io.LimitReader(stderrIn, 100)) - return nil, fmt.Errorf("failed to transcoding (%s) with args (%s) with error %w: %s", t.config.FfmpegBinPath, args, err, string(b)) + return nil, fmt.Errorf("failed to transcoding (%s) with args (%s) with error %w", t.config.FfmpegBinPath, args, err) } } @@ -286,6 +284,8 @@ func (t *Transcoder) GetMetadata() (transcoder.Metadata, error) { } var reEQ = regexp.MustCompile(`=\s+`) +var mgError = `Error ` +var mgFailed = `Conversion failed!` // progress sends through given channel the transcoding status func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress) { @@ -317,11 +317,18 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress buf := make([]byte, 2) scanner.Buffer(buf, bufio.MaxScanTokenSize) + var isFailed bool + var errMessages []string + for scanner.Scan() { Progress := new(Progress) line := scanner.Text() - - if strings.Contains(line, "time=") && strings.Contains(line, "bitrate=") { + //println(`========>`, `[`+line+`]`) + if strings.HasPrefix(line, mgError) { + errMessages = append(errMessages, line) + } else if strings.HasPrefix(line, mgFailed) { + isFailed = true + } else if strings.Contains(line, "time=") && strings.Contains(line, "bitrate=") { st := reEQ.ReplaceAllString(line, `=`) f := strings.Fields(st) var framesProcessed string @@ -368,6 +375,9 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress out <- *Progress } } + if isFailed && len(errMessages) > 0 { + out <- &Progress{Error: errors.New(strings.Join(errMessages, "\n"))} + } } // closePipes Closes pipes if opened