Skip to content

Commit

Permalink
Video: Add ffmpeg-bitrate config option photoprism#703
Browse files Browse the repository at this point in the history
  • Loading branch information
lastzero committed Feb 17, 2021
1 parent f326590 commit 4903936
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 33 deletions.
1 change: 1 addition & 0 deletions internal/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func configAction(ctx *cli.Context) error {
fmt.Printf("%-25s %s\n", "heifconvert-bin", conf.HeifConvertBin())
fmt.Printf("%-25s %s\n", "ffmpeg-bin", conf.FFmpegBin())
fmt.Printf("%-25s %s\n", "ffmpeg-encoder", conf.FFmpegEncoder())
fmt.Printf("%-25s %d\n", "ffmpeg-bitrate", conf.FFmpegBitrate())
fmt.Printf("%-25s %d\n", "ffmpeg-buffers", conf.FFmpegBuffers())
fmt.Printf("%-25s %s\n", "exiftool-bin", conf.ExifToolBin())

Expand Down
40 changes: 40 additions & 0 deletions internal/config/ffmpeg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package config

// FFmpegBin returns the ffmpeg executable file name.
func (c *Config) FFmpegBin() string {
return findExecutable(c.options.FFmpegBin, "ffmpeg")
}

// FFmpegEncoder returns the ffmpeg AVC encoder name.
func (c *Config) FFmpegEncoder() string {
if c.options.FFmpegEncoder == "" {
return "libx264"
}

return c.options.FFmpegEncoder
}

// FFmpegBuffers returns the number of ffmpeg capture buffers.
func (c *Config) FFmpegBuffers() int {
if c.options.FFmpegBuffers <= 8 {
return 8
}

if c.options.FFmpegBuffers >= 2048 {
return 2048
}

return c.options.FFmpegBuffers
}

// 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
}
}
6 changes: 6 additions & 0 deletions internal/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ var GlobalFlags = []cli.Flag{
Value: "libx264",
EnvVar: "PHOTOPRISM_FFMPEG_ENCODER",
},
cli.IntFlag{
Name: "ffmpeg-bitrate",
Usage: "FFmpeg encoding bitrate `LIMIT` in Mbit/s",
Value: 50,
EnvVar: "PHOTOPRISM_FFMPEG_BITRATE",
},
cli.IntFlag{
Name: "ffmpeg-buffers",
Usage: "FFmpeg capture buffers",
Expand Down
27 changes: 0 additions & 27 deletions internal/config/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,33 +250,6 @@ func (c *Config) SidecarWritable() bool {
return !c.ReadOnly() || c.SidecarPathIsAbs()
}

// FFmpegBin returns the ffmpeg executable file name.
func (c *Config) FFmpegBin() string {
return findExecutable(c.options.FFmpegBin, "ffmpeg")
}

// FFmpegEncoder returns the ffmpeg AVC encoder name.
func (c *Config) FFmpegEncoder() string {
if c.options.FFmpegEncoder == "" {
return "libx264"
}

return c.options.FFmpegEncoder
}

// FFmpegBuffers returns the number of ffmpeg capture buffers.
func (c *Config) FFmpegBuffers() int {
if c.options.FFmpegBuffers <= 8 {
return 8
}

if c.options.FFmpegBuffers >= 2048 {
return 2048
}

return c.options.FFmpegBuffers
}

// TempPath returns a temporary directory name for uploads and downloads.
func (c *Config) TempPath() string {
if c.options.TempPath == "" {
Expand Down
1 change: 1 addition & 0 deletions internal/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type Options struct {
HeifConvertBin string `yaml:"HeifConvertBin" json:"-" flag:"heifconvert-bin"`
FFmpegBin string `yaml:"FFmpegBin" json:"-" flag:"ffmpeg-bin"`
FFmpegEncoder string `yaml:"FFmpegEncoder" json:"FFmpegEncoder" flag:"ffmpeg-encoder"`
FFmpegBitrate int `yaml:"FFmpegBitrate" json:"FFmpegBitrate" flag:"ffmpeg-bitrate"`
FFmpegBuffers int `yaml:"FFmpegBuffers" json:"FFmpegBuffers" flag:"ffmpeg-buffers"`
ExifToolBin string `yaml:"ExifToolBin" json:"-" flag:"exiftool-bin"`
DetachServer bool `yaml:"DetachServer" json:"-" flag:"detach-server"`
Expand Down
12 changes: 7 additions & 5 deletions internal/photoprism/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,21 @@ func (c *Convert) ToJpeg(f *MediaFile) (*MediaFile, error) {

// AvcBitrate returns the ideal AVC encoding bitrate in megabits per second.
func (c *Convert) AvcBitrate(f *MediaFile) string {
const defaultBitrate = "8M"

if f == nil {
return "8M"
return defaultBitrate
}

// Baseline: 15
limit := c.conf.FFmpegBitrate()
quality := 12

bitrate := int(math.Ceil(float64(f.Width()*f.Height()*quality) / 1000000))

if bitrate <= 0 {
return "8M"
} else if bitrate >= 100 {
return "100M"
return defaultBitrate
} else if bitrate > limit {
bitrate = limit
}

return fmt.Sprintf("%dM", bitrate)
Expand Down
2 changes: 1 addition & 1 deletion internal/photoprism/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,6 @@ func TestConvert_AvcBitrate(t *testing.T) {
mf.width = 4096
mf.height = 2160

assert.Equal(t, "100M", convert.AvcBitrate(mf))
assert.Equal(t, "50M", convert.AvcBitrate(mf))
})
}

0 comments on commit 4903936

Please sign in to comment.