diff --git a/ffmpeg.go b/ffmpeg.go new file mode 100644 index 0000000..6da23e3 --- /dev/null +++ b/ffmpeg.go @@ -0,0 +1,50 @@ +package main + +import ( + "fmt" + "os/exec" + "time" +) + +func ffmpegGenerateMP4() { + inputPattern := "./output/img_glitched_%d.jpeg" + outputVideo := "output.mp4" + + cmd := exec.Command( + "ffmpeg", + "-y", + "-framerate", "30", + "-i", inputPattern, + "-vf", "scale=trunc(iw/2)*2:trunc(ih/2)*2,setpts=PTS*2.0", // Scale and slow down playback + "-vcodec", "libx264", // Use H.264 codec + "-pix_fmt", "yuv420p", + "-crf", "23", // Compression level: lower is higher quality (range: 18-28) + "-preset", "fast", + outputVideo, + ) + + done := make(chan bool) + + go func() { + err := cmd.Run() + check(err, "cannot run ffmpeg command!") + done <- true + }() + + go func() { + msg := "Generating MP4" + c := 0 + for { + select { + case <-done: + return + default: + fmt.Printf("\r%s%s", msg, dots(c)) + c = (c + 1) + time.Sleep(500 * time.Millisecond) + } + } + }() + <-done + fmt.Println("\nGIF successfully created in ./output.mp4!") +} diff --git a/glitch.go b/glitch.go index c4580f4..fd4adbe 100644 --- a/glitch.go +++ b/glitch.go @@ -7,16 +7,7 @@ import ( "path/filepath" ) -func makeOutputDir() { - // Default to just "output" - err := os.Mkdir("output", 0755) - check(err, "cannot make directory") -} - func glitchImage(inputFile string, outputFile string, shitSize int64, iterations int64) error { - // Ensure the output directory exists - makeOutputDir() - // Prepend the "output" directory to the output file outputFilePath := filepath.Join("output", outputFile) diff --git a/main.go b/main.go index 30643f4..5bbf16f 100644 --- a/main.go +++ b/main.go @@ -2,16 +2,13 @@ package main import ( "fmt" - "math/rand" "os" - "os/exec" "strconv" - "time" ) -func main() { - rand.Seed(time.Now().UnixNano()) +const MAX_SIZE = 1000000 +func main() { if len(os.Args) < 5 { fmt.Println("Usage: go run main.go ") return @@ -28,65 +25,16 @@ func main() { if err != nil || shitSize <= 0 { fmt.Println("Invalid shit size. Must be a positive integer.") return + } else if shitSize > MAX_SIZE { + fmt.Printf("shitSize exceeds maximum size (%d)\n", MAX_SIZE) + return } iterations, err := strconv.ParseInt(os.Args[4], 10, 8) + check(err, "Cannot parse iterations. Must be a positive integer") - /* - for i := 1; i <= numCopies; i++ { - outputFile := fmt.Sprintf("%s_glitched_%d%s", - stripExtension(sourceFile), - i, - filepath.Ext(sourceFile), - ) - - err := glitchImage(sourceFile, outputFile, shitSize, iterations) - if err != nil { - fmt.Printf("Error glitching file %d: %v\n", i, err) - } - } - */ + makeOutputDir() generateGlitchedSequence(sourceFile, numCopies, shitSize, iterations) - - inputPattern := "./output/img_glitched_%d.jpeg" - outputVideo := "output.mp4" - - cmd := exec.Command( - "ffmpeg", - "-y", - "-framerate", "30", - "-i", inputPattern, - "-vf", "scale=trunc(iw/2)*2:trunc(ih/2)*2,setpts=PTS*2.0", // Scale and slow down playback - "-vcodec", "libx264", // Use H.264 codec - "-pix_fmt", "yuv420p", - "-crf", "23", // Compression level: lower is higher quality (range: 18-28) - "-preset", "fast", - outputVideo, - ) - - done := make(chan bool) - - go func() { - err := cmd.Run() - check(err, "cannot run ffmpeg command!") - done <- true - }() - - go func() { - msg := "Generating MP4" - c := 0 - for { - select { - case <-done: - return - default: - fmt.Printf("\r%s%s", msg, dots(c)) - c = (c + 1) - time.Sleep(500 * time.Millisecond) - } - } - }() - <-done - fmt.Println("\nGIF successfully created in ./output.mp4!") - // + ffmpegGenerateMP4() + clearOutputDir() } diff --git a/util.go b/util.go index e397a8a..998e53c 100644 --- a/util.go +++ b/util.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "os" "path/filepath" ) @@ -23,3 +24,16 @@ func stripExtension(filename string) string { ext := filepath.Ext(filename) return filename[:len(filename)-len(ext)] } + +func makeOutputDir() { + // Default to just "output" + err := os.Mkdir("output", 0755) + check(err, "cannot make directory") + return +} + +func clearOutputDir() { + err := os.RemoveAll("./output") + check(err, "cannot remove directory") + return +}