| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 | package ffmpegutilimport (	"fmt"	"os"	"os/exec"	"path/filepath"	"imuslab.com/arozos/mod/utils")/*	FFmepg Utilities function	for agi.ffmpeg.go*//*ffmpeg_conv support input of a limited video, audio and image formatsCompression value can be set if compression / resize is needed.Different conversion type have different meaning for compression valuesVideo -> Video | compression means resolution in scale, e.g. 720 = 720pVideo / Audio -> Audio | compression means bitrate, e.g. 128 = 128kbpsImage -> Image | compression means final width of compression e.g. 1024 * 2048 with compression valueset to 512, then the output will be 512 * 1024Set compression to 0 if resizing / compression is not required*/func FFmpeg_conv(input string, output string, compression int) error {	var cmd *exec.Cmd	switch {	case isVideo(input) && isVideo(output):		// Video to video with resolution compression		if compression == 0 {			cmd = exec.Command("ffmpeg", "-i", input, output)		} else {			cmd = exec.Command("ffmpeg", "-i", input, "-vf", fmt.Sprintf("scale=-1:%d", compression), output)		}	case (isAudio(input) || isVideo(input)) && isAudio(output):		// Audio or video to audio with bitrate compression		if compression == 0 {			cmd = exec.Command("ffmpeg", "-i", input, output)		} else {			cmd = exec.Command("ffmpeg", "-i", input, "-b:a", fmt.Sprintf("%dk", compression), output)		}	case (isImage(input) && isImage(output)) || (isVideo(input) && filepath.Ext(output) == ".gif"):		// Resize image with width compression		if compression == 0 {			cmd = exec.Command("ffmpeg", "-i", input, output)		} else {			cmd = exec.Command("ffmpeg", "-i", input, "-vf", fmt.Sprintf("scale=%d:-1", compression), output)		}	default:		// Handle other cases or leave it for the user to implement		return fmt.Errorf("unsupported conversion: %s to %s", input, output)	}	// Set the output of the command to os.Stdout so you can see it in your console	cmd.Stdout = os.Stdout	// Set the output of the command to os.Stderr so you can see any errors	cmd.Stderr = os.Stderr	// Run the command	err := cmd.Run()	if err != nil {		return fmt.Errorf("error running ffmpeg command: %v", err)	}	return nil}// Helper functions to check file typesfunc isVideo(filename string) bool {	videoFormats := []string{		".mp4", ".mkv", ".avi", ".mov", ".flv", ".webm",	}	return utils.StringInArray(videoFormats, filepath.Ext(filename))}func isAudio(filename string) bool {	audioFormats := []string{		".mp3", ".wav", ".aac", ".ogg", ".flac",	}	return utils.StringInArray(audioFormats, filepath.Ext(filename))}func isImage(filename string) bool {	imageFormats := []string{		".jpg", ".png", ".gif", ".bmp", ".tiff", ".webp",	}	return utils.StringInArray(imageFormats, filepath.Ext(filename))}
 |