🚧 Trim with ffmpeg
This commit is contained in:
		
					parent
					
						
							
								a706d16c5a
							
						
					
				
			
			
				commit
				
					
						5b2896c032
					
				
			
		
					 3 changed files with 75 additions and 0 deletions
				
			
		
							
								
								
									
										16
									
								
								app/run.go
									
										
									
									
									
								
							
							
						
						
									
										16
									
								
								app/run.go
									
										
									
									
									
								
							|  | @ -23,7 +23,23 @@ func quit() { | ||||||
| 	os.Exit(0) | 	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) { | func Run(step AppStep) { | ||||||
|  | 	testTrim() | ||||||
| 	for step < Quit { | 	for step < Quit { | ||||||
| 		switch step { | 		switch step { | ||||||
| 		case Pick: | 		case Pick: | ||||||
|  |  | ||||||
|  | @ -16,6 +16,7 @@ type Config struct { | ||||||
| 	SavePath         string            `toml:"save_path"` | 	SavePath         string            `toml:"save_path"` | ||||||
| 	MusicPath        string            `toml:"music_path"` | 	MusicPath        string            `toml:"music_path"` | ||||||
| 	Codec            string            `toml:"codec"` | 	Codec            string            `toml:"codec"` | ||||||
|  | 	FfEncoder        string            `toml:"ff_encoder"` | ||||||
| 	AllowedCodecs    []string          `toml:"allowed_codec"` | 	AllowedCodecs    []string          `toml:"allowed_codec"` | ||||||
| 	CodecExt         map[string]string `toml:"codec_ext"` | 	CodecExt         map[string]string `toml:"codec_ext"` | ||||||
| } | } | ||||||
|  | @ -30,6 +31,7 @@ func newConfig() Config { | ||||||
| 		SavePath:         filepath.Join(xdg.UserDirs.Documents, "StripBeats"), | 		SavePath:         filepath.Join(xdg.UserDirs.Documents, "StripBeats"), | ||||||
| 		MusicPath:        xdg.UserDirs.Music, | 		MusicPath:        xdg.UserDirs.Music, | ||||||
| 		Codec:            "opus", | 		Codec:            "opus", | ||||||
|  | 		FfEncoder:        "libopus", | ||||||
| 		AllowedCodecs:    []string{"aac", "opus"}, | 		AllowedCodecs:    []string{"aac", "opus"}, | ||||||
| 		CodecExt:         map[string]string{"aac": "m4a", "opus": "opus"}, | 		CodecExt:         map[string]string{"aac": "m4a", "opus": "opus"}, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
							
								
								
									
										57
									
								
								media/ffmpeg.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								media/ffmpeg.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -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) | ||||||
|  | } | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue