2021-02-17 18:19:52 +01:00
|
|
|
package config
|
|
|
|
|
2023-03-14 18:00:55 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/ffmpeg"
|
2023-07-18 15:15:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/thumb"
|
2023-03-14 18:00:55 +01:00
|
|
|
)
|
2022-04-06 17:46:41 +02:00
|
|
|
|
2021-02-17 18:19:52 +01:00
|
|
|
// FFmpegBin returns the ffmpeg executable file name.
|
|
|
|
func (c *Config) FFmpegBin() string {
|
2023-09-06 11:27:24 +02:00
|
|
|
return findBin(c.options.FFmpegBin, ffmpeg.DefaultBin)
|
2021-02-17 18:19:52 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// FFmpegEnabled checks if FFmpeg is enabled for video transcoding.
|
2021-04-30 14:24:01 +02:00
|
|
|
func (c *Config) FFmpegEnabled() bool {
|
|
|
|
return !c.DisableFFmpeg()
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// FFmpegEncoder returns the FFmpeg AVC encoder name.
|
|
|
|
func (c *Config) FFmpegEncoder() ffmpeg.AvcEncoder {
|
2022-07-05 23:13:34 +02:00
|
|
|
if c.options.FFmpegEncoder == "" || c.options.FFmpegEncoder == ffmpeg.SoftwareEncoder.String() {
|
|
|
|
return ffmpeg.SoftwareEncoder
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
return ffmpeg.FindEncoder(c.options.FFmpegEncoder)
|
2021-02-17 18:19:52 +01:00
|
|
|
}
|
|
|
|
|
2023-07-18 15:15:04 +02:00
|
|
|
// FFmpegSize returns the maximum ffmpeg video encoding size in pixels (720-7680).
|
|
|
|
func (c *Config) FFmpegSize() int {
|
|
|
|
return thumb.VideoSize(c.options.FFmpegSize).Width
|
|
|
|
}
|
|
|
|
|
2021-02-17 18:19:52 +01:00
|
|
|
// FFmpegBitrate returns the ffmpeg bitrate limit in MBit/s.
|
|
|
|
func (c *Config) FFmpegBitrate() int {
|
|
|
|
switch {
|
|
|
|
case c.options.FFmpegBitrate <= 0:
|
|
|
|
return 50
|
|
|
|
case c.options.FFmpegBitrate >= 960:
|
|
|
|
return 960
|
|
|
|
default:
|
|
|
|
return c.options.FFmpegBitrate
|
|
|
|
}
|
|
|
|
}
|
2022-06-26 19:47:12 +02:00
|
|
|
|
|
|
|
// FFmpegBitrateExceeded tests if the ffmpeg bitrate limit is exceeded.
|
|
|
|
func (c *Config) FFmpegBitrateExceeded(mbit float64) bool {
|
|
|
|
if mbit <= 0 {
|
|
|
|
return false
|
|
|
|
} else if max := c.FFmpegBitrate(); max <= 0 {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return mbit > float64(max)
|
|
|
|
}
|
|
|
|
}
|
2023-03-14 18:00:55 +01:00
|
|
|
|
|
|
|
// FFmpegMapVideo returns the video streams to be transcoded as string.
|
|
|
|
func (c *Config) FFmpegMapVideo() string {
|
|
|
|
if c.options.FFmpegMapVideo == "" {
|
|
|
|
return ffmpeg.MapVideoDefault
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.options.FFmpegMapVideo
|
|
|
|
}
|
|
|
|
|
|
|
|
// FFmpegMapAudio returns the audio streams to be transcoded as string.
|
|
|
|
func (c *Config) FFmpegMapAudio() string {
|
|
|
|
if c.options.FFmpegMapAudio == "" {
|
|
|
|
return ffmpeg.MapAudioDefault
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.options.FFmpegMapAudio
|
|
|
|
}
|
|
|
|
|
|
|
|
// FFmpegOptions returns the FFmpeg transcoding options.
|
2023-06-29 10:07:47 +02:00
|
|
|
func (c *Config) FFmpegOptions(encoder ffmpeg.AvcEncoder, bitrate string) (ffmpeg.Options, error) {
|
2023-03-14 18:00:55 +01:00
|
|
|
// Transcode all other formats with FFmpeg.
|
|
|
|
opt := ffmpeg.Options{
|
2023-07-18 15:15:04 +02:00
|
|
|
Bin: c.FFmpegBin(),
|
|
|
|
Encoder: encoder,
|
|
|
|
Size: c.FFmpegSize(),
|
|
|
|
Bitrate: bitrate,
|
|
|
|
MapVideo: c.FFmpegMapVideo(),
|
|
|
|
MapAudio: c.FFmpegMapAudio(),
|
2023-03-14 18:00:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check
|
|
|
|
if opt.Bin == "" {
|
|
|
|
return opt, fmt.Errorf("ffmpeg is not installed")
|
|
|
|
} else if c.DisableFFmpeg() {
|
|
|
|
return opt, fmt.Errorf("ffmpeg is disabled")
|
|
|
|
} else if bitrate == "" {
|
|
|
|
return opt, fmt.Errorf("bitrate must not be empty")
|
|
|
|
} else if encoder.String() == "" {
|
|
|
|
return opt, fmt.Errorf("encoder must not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return opt, nil
|
|
|
|
}
|