package main import ( "flag" "fmt" "log" "os" "os/exec" "path/filepath" "strconv" "strings" "time" ) func GetVideoLength(ffprobeLocation string, fileName string) (seconds int, err error) { var out []byte out, err = exec.Command("/bin/sh", "-c", fmt.Sprintf("%s -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 \"%s\"", ffprobeLocation, fileName)).CombinedOutput() if err != nil { return } var secondsf float64 secondsf, err = strconv.ParseFloat(strings.TrimSuffix(string(out), "\n"), 64) if err != nil { return } seconds = int(secondsf) return } func ProcessFileName(filename string) string { return strings.TrimSuffix(filepath.Base(filename), ".mkv") } func main() { filename := flag.String("filename", "", "video to make clips of") raw_clip_length := flag.String("clip_length", "2s", "how many seconds long each clip should be") clips_folder := flag.String("clips_folder", "clips", "folder to store clips in") ffmpegLocation := flag.String("ffmpeg", "ffmpeg", "specify ffmpeg location if its not in path") ffprobeLocation := flag.String("ffprobe", "ffprobe", "specify ffprobe location if its not in path") flag.Parse() clip_lenght_f, err := time.ParseDuration(*raw_clip_length) if err != nil { log.Fatal(err) } clip_length := int(clip_lenght_f.Seconds()) if clip_length < 1 { log.Fatal("Please choose length longer than a second") } if *filename == "" { log.Fatalln("Please insert video to make clips of") } fmt.Println("Creating clips folder:", *clips_folder) if err := os.MkdirAll(*clips_folder, 0777); err != nil { log.Fatal(err) } length, err := GetVideoLength(*ffprobeLocation, *filename) if err != nil { log.Fatal(err) } for start := 0; start <= length; start += clip_length { fmt.Printf("Creating clip for seconds %d to %d\n", start, start+clip_length) out, err := exec.Command("/bin/sh", "-c", fmt.Sprintf("%s -ss %d -i \"%s\" -c copy -t %d \"%s/%s seconds %d to %d.mp4\"", *ffmpegLocation, start, *filename, clip_length, *clips_folder, ProcessFileName(*filename), start, start+clip_length)).CombinedOutput() if err != nil { fmt.Print(string(out)) log.Fatal(err) } } }