You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

43 lines
1.0 KiB

  1. package media
  2. import (
  3. "bytes"
  4. "fmt"
  5. "image"
  6. "os"
  7. "os/exec"
  8. "path"
  9. "path/filepath"
  10. )
  11. func VideoFrame(filename string) (image.Image, error) {
  12. cmd := exec.Command("ffmpeg", "-i", filename, "-vframes", "1", "-f", "singlejpeg", "-")
  13. buffer := new(bytes.Buffer)
  14. cmd.Stdout = buffer
  15. if cmd.Run() != nil {
  16. return nil, fmt.Errorf("could not generate frame")
  17. }
  18. img, _, err := image.Decode(buffer)
  19. return img, err
  20. }
  21. func VideoSize(filename string) (image.Rectangle, error) {
  22. img, err := VideoFrame(filename)
  23. if err != nil {
  24. return image.Rectangle{}, err
  25. }
  26. return img.Bounds(), nil
  27. }
  28. func VideoEncode(filename string, size image.Rectangle, thumbDir string) error {
  29. dest := thumbFilename(thumbDir, size, path.Base(filename))
  30. os.MkdirAll(filepath.Dir(dest), 0755)
  31. cmd := exec.Command("ffmpeg", "-i", filename, "-vf", fmt.Sprintf("scale=%d:%d", size.Dx(), size.Dy()), "-strict", "-2", dest)
  32. if out, err := cmd.CombinedOutput(); err != nil {
  33. os.Remove(dest)
  34. return fmt.Errorf("could not thumb video: %s", string(out))
  35. }
  36. return nil
  37. }