From 5b2896c0325dae7624bb473b3c9c8774de3bbb9a Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 8 Sep 2023 09:35:49 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Trim=20with=20ffmpeg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/run.go | 16 ++++++++++++++ config/config.go | 2 ++ media/ffmpeg.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 media/ffmpeg.go diff --git a/app/run.go b/app/run.go index 191b545..dfa6e44 100644 --- a/app/run.go +++ b/app/run.go @@ -23,7 +23,23 @@ func quit() { os.Exit(0) } +func testTrim() { + path := "/home/drj/MyFiles/Videos/WebShows/YouTube/Savannah_Outen/Love_Me_Like_You_Do_-_Ellie_Goulding_Cover_by_Savannah_Outen-tPYfBpRxXfI.mp4" + file := SetFile(path) + tmp, _ := os.CreateTemp("", "audio.*.mka") + tmp.Close() + // err := media.TrimWithFade(file, tmp.Name(), 0, 215.84, 0, 3) + // err := media.Trim(file, tmp.Name(), 0, 215.84) + err := media.TrimWithFade(file, tmp.Name(), 15.2, 70.84, 3.6, 8.4) + if err != nil { + panic(err) + } + fmt.Println(tmp.Name()) + quit() +} + func Run(step AppStep) { + testTrim() for step < Quit { switch step { case Pick: diff --git a/config/config.go b/config/config.go index 3831883..4ca54bc 100644 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,7 @@ type Config struct { SavePath string `toml:"save_path"` MusicPath string `toml:"music_path"` Codec string `toml:"codec"` + FfEncoder string `toml:"ff_encoder"` AllowedCodecs []string `toml:"allowed_codec"` CodecExt map[string]string `toml:"codec_ext"` } @@ -30,6 +31,7 @@ func newConfig() Config { SavePath: filepath.Join(xdg.UserDirs.Documents, "StripBeats"), MusicPath: xdg.UserDirs.Music, Codec: "opus", + FfEncoder: "libopus", AllowedCodecs: []string{"aac", "opus"}, CodecExt: map[string]string{"aac": "m4a", "opus": "opus"}, } diff --git a/media/ffmpeg.go b/media/ffmpeg.go new file mode 100644 index 0000000..9dfa4c2 --- /dev/null +++ b/media/ffmpeg.go @@ -0,0 +1,57 @@ +package media + +import ( + "errors" + "fmt" + "strconv" + + "codeberg.org/danjones000/strip-beats/config" + ffmpeg "github.com/u2takey/ffmpeg-go" +) + +func TrimWithFade(in Probe, out string, start, stop, up, down float64) error { + // -ss (start) -t (end) -af afade=t=in:st=(start):d=(up),afade=t=out:st=(downstart):d=(down) + st := in.GetFirstAcceptableAudio() + if st == nil { + return errors.New("Can't find an audio stream") + } + args := ffmpeg.KwArgs{"c:a": "copy"} + if start != 0 { + args["ss"] = start + } + if stop != 0 { + end := stop - start + args["t"] = end + } else { + stop = st.Duration + } + + input := ffmpeg.Input(in.Format.Path).Get(strconv.Itoa(st.Index)) + if up != 0 { + args["c:a"] = config.GetConfig().FfEncoder + input = input.Filter("afade", ffmpeg.Args{ + "t=in", + fmt.Sprintf("st=%v", start), + fmt.Sprintf("d=%v", up)}) + } + + if down != 0 { + args["c:a"] = config.GetConfig().FfEncoder + downstart := stop - down + input = input.Filter("afade", ffmpeg.Args{ + "t=out", + fmt.Sprintf("st=%v", downstart), + fmt.Sprintf("d=%v", down)}) + } + output := input.Output(out, args).GlobalArgs("-y") + // @todo out should be stdout + return output.Run() +} + +func Trim(in Probe, out string, start, stop float64) error { + return TrimWithFade(in, out, start, stop, 0, 0) +} + +func Fade(in Probe, out string, up, down float64) error { + return TrimWithFade(in, out, 0, 0, up, down) +}