run gofumpt / format comments / comment blocks
This commit is contained in:
@@ -2,10 +2,9 @@ package encode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
@@ -30,20 +29,19 @@ var (
|
||||
bufLen = 4096
|
||||
)
|
||||
|
||||
// Copy command output to HTTP response body using io.Copy (simpler, but may increase TTFB)
|
||||
// copy command output to http response body using io.copy (simpler, but may increase ttfb)
|
||||
//nolint:deadcode,unused
|
||||
func copyCmdOutput(out, cache io.Writer, pipeReader io.Reader) {
|
||||
// Set up a MultiWriter to feed the command output
|
||||
// to both cache file and HTTP response:
|
||||
// set up a multiwriter to feed the command output
|
||||
// to both cache file and http response
|
||||
w := io.MultiWriter(out, cache)
|
||||
|
||||
// Start copying!
|
||||
// start copying!
|
||||
if _, err := io.Copy(w, pipeReader); err != nil {
|
||||
log.Printf("error while writing encoded output: %s\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Copy command output to HTTP response manually with a buffer (should reduce TTFB)
|
||||
// copy command output to http response manually with a buffer (should reduce ttfb)
|
||||
//nolint:deadcode,unused
|
||||
func writeCmdOutput(out, cache io.Writer, pipeReader io.ReadCloser) {
|
||||
buffer := make([]byte, bufLen)
|
||||
@@ -53,18 +51,15 @@ func writeCmdOutput(out, cache io.Writer, pipeReader io.ReadCloser) {
|
||||
pipeReader.Close()
|
||||
break
|
||||
}
|
||||
|
||||
data := buffer[0:n]
|
||||
_, err = out.Write(data)
|
||||
if err != nil {
|
||||
log.Printf("error while writing HTTP response: %s\n", err)
|
||||
}
|
||||
|
||||
_, err = cache.Write(data)
|
||||
if err != nil {
|
||||
log.Printf("error while writing cache file: %s\n", err)
|
||||
}
|
||||
|
||||
if f, ok := out.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
}
|
||||
@@ -75,7 +70,7 @@ func writeCmdOutput(out, cache io.Writer, pipeReader io.ReadCloser) {
|
||||
}
|
||||
}
|
||||
|
||||
// Pre-format the FFmpeg command with needed options:
|
||||
// pre-format the ffmpeg command with needed options
|
||||
func ffmpegCommand(filePath string, profile *Profile, bitrate string) *exec.Cmd {
|
||||
ffmpegArgs := []string{
|
||||
"-v", "0", "-i", filePath, "-map", "0:0",
|
||||
@@ -84,9 +79,9 @@ func ffmpegCommand(filePath string, profile *Profile, bitrate string) *exec.Cmd
|
||||
ffmpegArgs = append(ffmpegArgs, profile.ffmpegOptions...)
|
||||
if profile.forceRG {
|
||||
ffmpegArgs = append(ffmpegArgs,
|
||||
// Set up ReplayGain processing
|
||||
// set up replaygain processing
|
||||
"-af", "volume=replaygain=track:replaygain_preamp=6dB:replaygain_noclip=0, alimiter=level=disabled",
|
||||
// Drop redundant ReplayGain tags
|
||||
// drop redundant replaygain tags
|
||||
"-metadata", "replaygain_album_gain=",
|
||||
"-metadata", "replaygain_album_peak=",
|
||||
"-metadata", "replaygain_track_gain=",
|
||||
@@ -94,35 +89,32 @@ func ffmpegCommand(filePath string, profile *Profile, bitrate string) *exec.Cmd
|
||||
)
|
||||
}
|
||||
ffmpegArgs = append(ffmpegArgs, "-f", profile.Format, "-")
|
||||
|
||||
return exec.Command("/usr/bin/ffmpeg", ffmpegArgs...)
|
||||
}
|
||||
|
||||
func Encode(out io.Writer, trackPath, cachePath string, profile *Profile, bitrate string) error {
|
||||
// Prepare the command and file descriptors:
|
||||
// prepare the command and file descriptors
|
||||
cmd := ffmpegCommand(trackPath, profile, bitrate)
|
||||
pipeReader, pipeWriter := io.Pipe()
|
||||
cmd.Stdout = pipeWriter
|
||||
cmd.Stderr = pipeWriter
|
||||
|
||||
// Create cache file:
|
||||
// create cache file
|
||||
cacheFile, err := os.Create(cachePath)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "writing to cache file %q: %v", cachePath)
|
||||
return errors.Wrapf(err, "writing to cache file %q: %v", cachePath, err)
|
||||
}
|
||||
|
||||
//// I'm still unsure if buffer version (writeCmdOutput) is any better than io.Copy-based one (copyCmdOutput).
|
||||
//// My initial goal here is to start streaming response ASAP, with smallest TTFB. More testing needed. -- @spijet
|
||||
// Start up writers for cache file and HTTP response:
|
||||
// still unsure if buffer version (writeCmdOutput) is any better than io.Copy-based one (copyCmdOutput)
|
||||
// initial goal here is to start streaming response asap, with smallest ttfb. more testing needed
|
||||
// -- @spijet
|
||||
//
|
||||
// start up writers for cache file and http response
|
||||
// go copyCmdOutput(w, cacheFile, pipeReader)
|
||||
go writeCmdOutput(out, cacheFile, pipeReader)
|
||||
|
||||
// Run FFmpeg:
|
||||
// run ffmpeg
|
||||
if err := cmd.Run(); err != nil {
|
||||
return errors.Wrapf(err, "running ffmpeg")
|
||||
}
|
||||
|
||||
// Close all pipes and flush cache file:
|
||||
// close all pipes and flush cache file
|
||||
pipeWriter.Close()
|
||||
if err := cacheFile.Sync(); err != nil {
|
||||
return errors.Wrapf(err, "flushing %q", cachePath)
|
||||
@@ -131,14 +123,14 @@ func Encode(out io.Writer, trackPath, cachePath string, profile *Profile, bitrat
|
||||
return nil
|
||||
}
|
||||
|
||||
// Generate cache key (file name). For, you know, encoded tracks cache.
|
||||
// generate cache key (file name). for, you know, encoded tracks cache
|
||||
func CacheKey(sourcePath string, profile, bitrate string) string {
|
||||
format := Profiles[profile].Format
|
||||
hash := xxhash.Sum64String(sourcePath)
|
||||
return fmt.Sprintf("%x-%s-%s.%s", hash, profile, bitrate, format)
|
||||
}
|
||||
|
||||
// Check if client forces bitrate lower than set in profile:
|
||||
// check if client forces bitrate lower than set in profile
|
||||
func GetBitrate(clientBitrate int, profile *Profile) string {
|
||||
bitrate := profile.Bitrate
|
||||
if clientBitrate != 0 && clientBitrate < bitrate {
|
||||
|
||||
Reference in New Issue
Block a user