Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion ffmpeg/presets/hls_1080p.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

set -ex

SCALE_FILTER=${SCALE_FILTER:-scale}

if [ "$SCALE_FILTER" = "zscale" ]; then
FILTER_OPTS="zscale=-2:1080:min=bt709:m=bt709"
else
FILTER_OPTS="scale=-2:1080"
fi

ffmpeg -i "$input_video" \
-vf "scale=-2:1080" \
-vf "$FILTER_OPTS" \
-c:v libx264 \
-b:v 4000k \
-maxrate 4000k \
Expand Down
10 changes: 9 additions & 1 deletion ffmpeg/presets/hls_480p.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

set -ex

SCALE_FILTER=${SCALE_FILTER:-scale}

if [ "$SCALE_FILTER" = "zscale" ]; then
FILTER_OPTS="zscale=-2:480:min=bt709:m=bt709"
else
FILTER_OPTS="scale=-2:480"
fi

ffmpeg -i "$input_video" \
-vf "scale=-2:480" \
-vf "$FILTER_OPTS" \
-c:v libx264 \
-b:v 700k \
-maxrate 700k \
Expand Down
10 changes: 9 additions & 1 deletion ffmpeg/presets/hls_720p.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

set -ex

SCALE_FILTER=${SCALE_FILTER:-scale}

if [ "$SCALE_FILTER" = "zscale" ]; then
FILTER_OPTS="zscale=-2:720:min=bt709:m=bt709"
else
FILTER_OPTS="scale=-2:720"
fi

ffmpeg -i "$input_video" \
-vf "scale=-2:720" \
-vf "$FILTER_OPTS" \
-c:v libx264 \
-b:v 1000k \
-maxrate 1000k \
Expand Down
39 changes: 39 additions & 0 deletions src/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"os/exec"
"strconv"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -101,16 +102,54 @@ func downloadVideo(job Job, videoPath string) error {
return downloadFromS3(job.Request.S3VideoURI, videoPath)
}

func detectVideoCodec(job Job, videoPath string) (string, error) {
log.Printf("[jid: %s] Detecting video codec...\n", job.JobID)

cmd := exec.Command("ffprobe",
"-v", "error",
"-select_streams", "v:0",
"-show_entries", "stream=codec_name",
"-of", "default=nw=1:nk=1",
videoPath)

output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to detect codec: %v", err)
}

codec := strings.TrimSpace(string(output))
log.Printf("[jid: %s] Detected codec: %s\n", job.JobID, codec)

return codec, nil
}

func determineScaleFilter(codec string) string {
if codec == "dxv" {
return "zscale"
}
return "scale"
}

func runConversionScript(job Job, inputPath, outputDir string) error {
log.Printf("[jid: %s] Running conversion script...\n", job.JobID)

codec, err := detectVideoCodec(job, inputPath)
scaleFilter := "scale"
if err != nil {
log.Printf("[jid: %s] Warning: codec detection failed (%v), defaulting to scale filter\n", job.JobID, err)
} else {
scaleFilter = determineScaleFilter(codec)
log.Printf("[jid: %s] Using %s filter for codec %s\n", job.JobID, scaleFilter, codec)
}

cmd := exec.Command("./ffmpeg/convert-video.sh")
cmd.Env = append(os.Environ(),
fmt.Sprintf("input_video=%s", inputPath),
fmt.Sprintf("output_dir=%s", outputDir),
fmt.Sprintf("hls_480p=%d", boolToInt(contains(job.Request.Presets, "hls_480p"))),
fmt.Sprintf("hls_720p=%d", boolToInt(contains(job.Request.Presets, "hls_720p"))),
fmt.Sprintf("hls_1080p=%d", boolToInt(contains(job.Request.Presets, "hls_1080p"))),
fmt.Sprintf("SCALE_FILTER=%s", scaleFilter),
)

if err := cmd.Run(); err != nil {
Expand Down