clean up and guard checks

main
Simon Kellet 1 week ago
parent bb6177811b
commit 51d7b25cba
  1. 50
      ffmpeg.go
  2. 9
      glitch.go
  3. 70
      main.go
  4. 14
      util.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!")
}

@ -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)

@ -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 <source-file> <number-of-copies> <shit-size> <iterations>")
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()
}

@ -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
}

Loading…
Cancel
Save