58 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
		
		
			
		
	
	
			58 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
|  | 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) | ||
|  | } |