ffmpegutil.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package ffmpegutil
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "imuslab.com/arozos/mod/utils"
  8. )
  9. /*
  10. FFmepg Utilities function
  11. for agi.ffmpeg.go
  12. */
  13. /*
  14. ffmpeg_conv support input of a limited video, audio and image formats
  15. Compression value can be set if compression / resize is needed.
  16. Different conversion type have different meaning for compression values
  17. Video -> Video | compression means resolution in scale, e.g. 720 = 720p
  18. Video / Audio -> Audio | compression means bitrate, e.g. 128 = 128kbps
  19. Image -> Image | compression means final width of compression e.g. 1024 * 2048 with compression value
  20. set to 512, then the output will be 512 * 1024
  21. Set compression to 0 if resizing / compression is not required
  22. */
  23. func FFmpeg_conv(input string, output string, compression int) error {
  24. var cmd *exec.Cmd
  25. switch {
  26. case isVideo(input) && isVideo(output):
  27. // Video to video with resolution compression
  28. if compression == 0 {
  29. cmd = exec.Command("ffmpeg", "-i", input, output)
  30. } else {
  31. cmd = exec.Command("ffmpeg", "-i", input, "-vf", fmt.Sprintf("scale=-1:%d", compression), output)
  32. }
  33. case (isAudio(input) || isVideo(input)) && isAudio(output):
  34. // Audio or video to audio with bitrate compression
  35. if compression == 0 {
  36. cmd = exec.Command("ffmpeg", "-i", input, output)
  37. } else {
  38. cmd = exec.Command("ffmpeg", "-i", input, "-b:a", fmt.Sprintf("%dk", compression), output)
  39. }
  40. case (isImage(input) && isImage(output)) || (isVideo(input) && filepath.Ext(output) == ".gif"):
  41. // Resize image with width compression
  42. if compression == 0 {
  43. cmd = exec.Command("ffmpeg", "-i", input, output)
  44. } else {
  45. cmd = exec.Command("ffmpeg", "-i", input, "-vf", fmt.Sprintf("scale=%d:-1", compression), output)
  46. }
  47. default:
  48. // Handle other cases or leave it for the user to implement
  49. return fmt.Errorf("unsupported conversion: %s to %s", input, output)
  50. }
  51. // Set the output of the command to os.Stdout so you can see it in your console
  52. cmd.Stdout = os.Stdout
  53. // Set the output of the command to os.Stderr so you can see any errors
  54. cmd.Stderr = os.Stderr
  55. // Run the command
  56. err := cmd.Run()
  57. if err != nil {
  58. return fmt.Errorf("error running ffmpeg command: %v", err)
  59. }
  60. return nil
  61. }
  62. // Helper functions to check file types
  63. func isVideo(filename string) bool {
  64. videoFormats := []string{
  65. ".mp4", ".mkv", ".avi", ".mov", ".flv", ".webm",
  66. }
  67. return utils.StringInArray(videoFormats, filepath.Ext(filename))
  68. }
  69. func isAudio(filename string) bool {
  70. audioFormats := []string{
  71. ".mp3", ".wav", ".aac", ".ogg", ".flac",
  72. }
  73. return utils.StringInArray(audioFormats, filepath.Ext(filename))
  74. }
  75. func isImage(filename string) bool {
  76. imageFormats := []string{
  77. ".jpg", ".png", ".gif", ".bmp", ".tiff", ".webp",
  78. }
  79. return utils.StringInArray(imageFormats, filepath.Ext(filename))
  80. }