fix crummy ffprobe json output handling
All checks were successful
/ build-linux-amd64 (push) Successful in 1m35s

This commit is contained in:
ari melody 2026-01-31 04:04:32 +00:00
parent f4381f7cdc
commit 51a1de845f
Signed by: ari
GPG key ID: CF99829C92678188
2 changed files with 42 additions and 16 deletions

24
main.go
View file

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"math"
"net/http" "net/http"
"os" "os"
"path" "path"
@ -247,18 +248,21 @@ func main() {
// concatenate VOD segments into full VOD // concatenate VOD segments into full VOD
fullVodExists := func () bool { fullVodExists := func () bool {
// check if full VOD already exists with expected duration // check if full VOD already exists with expected duration
if fullVodProbe, err := scanner.ProbeSegment(video.Filename); err != nil { fullVodProbe, err := scanner.ProbeSegment(video.Filename)
var totalLength float64 = 0 if err != nil { return false }
for _, filename := range vodFiles { video.SizeBytes = fullVodProbe.Format.Size
probe, err := scanner.ProbeSegment(filename) var totalLength float64 = 0
if err != nil { continue }
totalLength += probe.Format.Duration for _, filename := range vodFiles {
} probe, err := scanner.ProbeSegment(path.Join(metadata.FootageDir, filename))
return fullVodProbe.Format.Duration == totalLength if err != nil { continue }
totalLength += probe.Format.Duration
} }
return false return math.Abs(fullVodProbe.Format.Duration - totalLength) < float64(0.1)
}() }()
if !fullVodExists { if fullVodExists {
log.Print("Full VOD appears to already exist- uploading this file...")
} else {
video.SizeBytes, err = vid.ConcatVideo(video, vodFiles, verbose) video.SizeBytes, err = vid.ConcatVideo(video, vodFiles, verbose)
if err != nil { if err != nil {
log.Fatalf("Failed to concatenate VOD segments: %v", err) log.Fatalf("Failed to concatenate VOD segments: %v", err)

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"os" "os"
"path" "path"
"strconv"
"strings" "strings"
"time" "time"
@ -28,13 +29,13 @@ type (
Category *Category `toml:"category" comment:"(Optional) Category details, for additional credits."` Category *Category `toml:"category" comment:"(Optional) Category details, for additional credits."`
} }
ffprobeFormat struct { FFprobeFormat struct {
Duration float64 `json:"duration"` Duration float64 `json:"duration"`
Size int64 `json:"size"` Size int64 `json:"size"`
} }
ffprobeOutput struct { FFprobeOutput struct {
Format ffprobeFormat `json:"format"` Format FFprobeFormat `json:"format"`
} }
) )
@ -58,15 +59,36 @@ func ScanSegments(directory string, extension string) ([]string, error) {
return files, nil return files, nil
} }
func ProbeSegment(filename string) (*ffprobeOutput, error) { func ProbeSegment(filename string) (*FFprobeOutput, error) {
out, err := ffmpeg_go.Probe(filename) out, err := ffmpeg_go.Probe(filename)
if err != nil { return nil, err } if err != nil { return nil, err }
probe := ffprobeOutput{} type (
RawFFprobeFormat struct {
// these being strings upsets me immensely
Duration string `json:"duration"`
Size string `json:"size"`
}
RawFFprobeOutput struct {
Format RawFFprobeFormat `json:"format"`
}
)
probe := RawFFprobeOutput{}
err = json.Unmarshal([]byte(out), &probe) err = json.Unmarshal([]byte(out), &probe)
if err != nil { return nil, err } if err != nil { return nil, err }
return &probe, nil duration, err := strconv.ParseFloat(probe.Format.Duration, 64)
if err != nil { return nil, err }
size, err := strconv.ParseInt(probe.Format.Size, 10, 0)
if err != nil { return nil, err }
return &FFprobeOutput{
Format: FFprobeFormat{
Duration: duration,
Size: size,
},
}, nil
} }
func ReadMetadata(directory string) (*Metadata, error) { func ReadMetadata(directory string) (*Metadata, error) {