Compare commits
	
		
			2 Commits
		
	
	
		
			c6ffd29fe9
			...
			bb6177811b
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					bb6177811b | ||
| 
						 | 
					e4a5771e18 | 
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@ -1,4 +1,5 @@
 | 
				
			|||||||
*.gif
 | 
					*.gif
 | 
				
			||||||
*.jpeg
 | 
					*.jpeg
 | 
				
			||||||
*.png
 | 
					*.png
 | 
				
			||||||
 | 
					*.mp4
 | 
				
			||||||
.DS_Store
 | 
					.DS_Store
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										10
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								README.md
									
									
									
									
									
								
							@ -16,15 +16,15 @@ go build
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
2. Run the command
 | 
					2. Run the command
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   ```bash
 | 
					```bash
 | 
				
			||||||
   $ ./glitch_img
 | 
					$ ./glitch_img
 | 
				
			||||||
   Usage: go run main.go <source-file> <number-of-copies> <shit-size> <iterations>
 | 
					Usage: go run main.go <source-file> <number-of-copies> <shit-size> <iterations>
 | 
				
			||||||
   ```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Example
 | 
					### Example
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```bash
 | 
					```bash
 | 
				
			||||||
./glitch*img img.jpeg 100 30 2
 | 
					./glitch_img img.jpeg 100 30 2
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Outputs 100 images into a destination folder _output_ with 30 random bytes of data, running through the file twice
 | 
					Outputs 100 images into a destination folder _output_ with 30 random bytes of data, running through the file twice
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										43
									
								
								glitch.go
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								glitch.go
									
									
									
									
									
								
							@ -1,6 +1,7 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"math/rand/v2"
 | 
						"math/rand/v2"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
@ -54,7 +55,47 @@ func glitchImage(inputFile string, outputFile string, shitSize int64, iterations
 | 
				
			|||||||
		_, err = destination.Write(shit)
 | 
							_, err = destination.Write(shit)
 | 
				
			||||||
		check(err, "failed to write shit data: %w")
 | 
							check(err, "failed to write shit data: %w")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// fmt.Printf("Successfully wrote %d bytes of shit at offset %d in file %s\n", shitSize, offset, outputFilePath)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func copyImage(inputFile string, outputFile string) error {
 | 
				
			||||||
 | 
						// Ensure the output directory exists
 | 
				
			||||||
 | 
						makeOutputDir()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Prepend the "output" directory to the output file
 | 
				
			||||||
 | 
						outputFilePath := filepath.Join("output", outputFile)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Open the source file for reading
 | 
				
			||||||
 | 
						source, err := os.Open(inputFile)
 | 
				
			||||||
 | 
						check(err, "failed to open source file: %w")
 | 
				
			||||||
 | 
						defer source.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Create a new file for writing the copy
 | 
				
			||||||
 | 
						destination, err := os.Create(outputFilePath)
 | 
				
			||||||
 | 
						check(err, "failed to create output file: %w")
 | 
				
			||||||
 | 
						defer destination.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Copy the contents of the source file to the destination
 | 
				
			||||||
 | 
						_, err = destination.ReadFrom(source)
 | 
				
			||||||
 | 
						check(err, "failed to copy file contents: %w")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func generateGlitchedSequence(inputFile string, totalImages int, shitSize int64, iterations int64) {
 | 
				
			||||||
 | 
						for i := 1; i <= totalImages; i++ {
 | 
				
			||||||
 | 
							outputFileName := fmt.Sprintf("img_glitched_%d.jpeg", i) // Generate unique filenames
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Randomly decide whether to glitch or copy the image
 | 
				
			||||||
 | 
							if rand.Int32N(2) == 0 {
 | 
				
			||||||
 | 
								// Glitched image
 | 
				
			||||||
 | 
								err := glitchImage(inputFile, outputFileName, shitSize, iterations)
 | 
				
			||||||
 | 
								check(err, "failed to glitch image: %w")
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								// Normal image
 | 
				
			||||||
 | 
								err := copyImage(inputFile, outputFileName)
 | 
				
			||||||
 | 
								check(err, "failed to copy normal image: %w")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										
											BIN
										
									
								
								glitch_img
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								glitch_img
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										73
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										73
									
								
								main.go
									
									
									
									
									
								
							@ -2,13 +2,16 @@ package main
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"math/rand"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"os/exec"
 | 
						"os/exec"
 | 
				
			||||||
	"path/filepath"
 | 
					 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
 | 
						rand.Seed(time.Now().UnixNano())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(os.Args) < 5 {
 | 
						if len(os.Args) < 5 {
 | 
				
			||||||
		fmt.Println("Usage: go run main.go <source-file> <number-of-copies> <shit-size> <iterations>")
 | 
							fmt.Println("Usage: go run main.go <source-file> <number-of-copies> <shit-size> <iterations>")
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
@ -29,23 +32,61 @@ func main() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	iterations, err := strconv.ParseInt(os.Args[4], 10, 8)
 | 
						iterations, err := strconv.ParseInt(os.Args[4], 10, 8)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for i := 1; i <= numCopies; i++ {
 | 
						/*
 | 
				
			||||||
		outputFile := fmt.Sprintf("%s_glitched_%d%s",
 | 
							for i := 1; i <= numCopies; i++ {
 | 
				
			||||||
			stripExtension(sourceFile),
 | 
								outputFile := fmt.Sprintf("%s_glitched_%d%s",
 | 
				
			||||||
			i,
 | 
									stripExtension(sourceFile),
 | 
				
			||||||
			filepath.Ext(sourceFile),
 | 
									i,
 | 
				
			||||||
		)
 | 
									filepath.Ext(sourceFile),
 | 
				
			||||||
 | 
								)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		err := glitchImage(sourceFile, outputFile, shitSize, iterations)
 | 
									err := glitchImage(sourceFile, outputFile, shitSize, iterations)
 | 
				
			||||||
		if err != nil {
 | 
									if err != nil {
 | 
				
			||||||
			fmt.Printf("Error glitching file %d: %v\n", i, err)
 | 
										fmt.Printf("Error glitching file %d: %v\n", i, err)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						*/
 | 
				
			||||||
 | 
						generateGlitchedSequence(sourceFile, numCopies, shitSize, iterations)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fmt.Println("GENREATING GIF YEAHH ")
 | 
						inputPattern := "./output/img_glitched_%d.jpeg"
 | 
				
			||||||
	// now run ffmpeg to make a gif!
 | 
						outputVideo := "output.mp4"
 | 
				
			||||||
	cmd := exec.Command("ffmpeg", "-y", "-framerate 60", "-i", "./output/img_glitched_%d.jpeg output.gif")
 | 
					
 | 
				
			||||||
	err = cmd.Run()
 | 
						cmd := exec.Command(
 | 
				
			||||||
	check(err, "cannot run ffmpeg 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!")
 | 
				
			||||||
	//
 | 
						//
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										8
									
								
								util.go
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								util.go
									
									
									
									
									
								
							@ -11,6 +11,14 @@ func check(e error, s string) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func dots(count int) string {
 | 
				
			||||||
 | 
						dotStr := ""
 | 
				
			||||||
 | 
						for i := 0; i < count; i++ {
 | 
				
			||||||
 | 
							dotStr += "."
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return dotStr
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func stripExtension(filename string) string {
 | 
					func stripExtension(filename string) string {
 | 
				
			||||||
	ext := filepath.Ext(filename)
 | 
						ext := filepath.Ext(filename)
 | 
				
			||||||
	return filename[:len(filename)-len(ext)]
 | 
						return filename[:len(filename)-len(ext)]
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user