First commit
This commit is contained in:
commit
e9ce932bf3
4 changed files with 77 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
clips/
|
||||
.DS_Store
|
3
README.md
Normal file
3
README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# clip-maker
|
||||
|
||||
Simple program to split video file into clips of requested length
|
3
go.mod
Normal file
3
go.mod
Normal file
|
@ -0,0 +1,3 @@
|
|||
module clip-maker
|
||||
|
||||
go 1.20
|
69
main.go
Normal file
69
main.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetVideoLength(fileName string) (seconds int, err error) {
|
||||
// var path string
|
||||
// path, err = filepath.Abs(fileName)
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
|
||||
var out []byte
|
||||
out, err = exec.Command("/bin/sh", "-c", fmt.Sprintf("ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 \"%s\"", 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")
|
||||
clip_length := flag.Int("clip_length", 2, "how long each clip should be")
|
||||
clips_folder := flag.String("clips_folder", "clips", "folder to store clips in")
|
||||
flag.Parse()
|
||||
|
||||
if *filename == "" {
|
||||
log.Fatalln("Please insert video to make clips of")
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(*clips_folder, 0777); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
length, err := GetVideoLength(*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("ffmpeg -ss %d -i \"%s\" -c copy -t %d \"%s/%s seconds %d to %d.mp4\"", start, *filename, clip_length, *clips_folder, ProcessFileName(*filename), start, start+*clip_length)).CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Print(string(out))
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue