add --dry-run option

This commit is contained in:
ari melody 2026-06-28 13:35:06 +01:00
parent f6e42cd7e6
commit 0ac59c5407
Signed by: ari
GPG key ID: 60B5F0386E3DDB7E
2 changed files with 57 additions and 42 deletions

98
main.go
View file

@ -77,6 +77,7 @@ func main() {
var logout bool = false var logout bool = false
var deleteFullVod bool = false var deleteFullVod bool = false
var forceUpload bool = false var forceUpload bool = false
var dryRun bool = false
var directory string = "." var directory string = "."
for i, arg := range os.Args { for i, arg := range os.Args {
@ -110,6 +111,9 @@ func main() {
case "--force": case "--force":
forceUpload = true forceUpload = true
case "--dry-run":
dryRun = true
default: default:
fmt.Fprintf(os.Stderr, "Unknown option `%s`\n", arg) fmt.Fprintf(os.Stderr, "Unknown option `%s`\n", arg)
os.Exit(1) os.Exit(1)
@ -273,29 +277,35 @@ func main() {
) )
} }
// concatenate VOD segments into full VOD if (!dryRun) {
fullVodExists := func () bool {
// check if full VOD already exists with expected duration // check if full VOD already exists with expected duration
fullVodExists := false
fullVodProbe, err := scanner.ProbeSegment(video.Filepath) fullVodProbe, err := scanner.ProbeSegment(video.Filepath)
if err != nil { return false } if err == nil {
video.SizeBytes = fullVodProbe.Format.Size video.SizeBytes = fullVodProbe.Format.Size
var totalLength float64 = 0 var totalLength float64 = 0
for _, filename := range vodFiles { for _, filename := range vodFiles {
probe, err := scanner.ProbeSegment(path.Join(footageDir, filename)) probe, err := scanner.ProbeSegment(path.Join(footageDir, filename))
if err != nil { continue } if err != nil { continue }
totalLength += probe.Format.Duration totalLength += probe.Format.Duration
}
// full VOD may exist, but not be complete (interrupted concat)
fullVodExists = math.Abs(fullVodProbe.Format.Duration - totalLength) < float64(0.1)
}
if fullVodExists {
log.Print("Full VOD appears to already exist- uploading this file...")
} else {
// concatenate VOD segments into full VOD
video.SizeBytes, err = vid.ConcatVideo(video, vodFiles, verbose)
if err != nil {
log.Fatalf("Failed to concatenate VOD segments: %v", err)
os.Exit(1)
}
} }
return math.Abs(fullVodProbe.Format.Duration - totalLength) < float64(0.1)
}()
if fullVodExists {
log.Print("Full VOD appears to already exist- uploading this file...")
} else { } else {
video.SizeBytes, err = vid.ConcatVideo(video, vodFiles, verbose) log.Print("Dry run: Skipping VOD concatenation")
if err != nil {
log.Fatalf("Failed to concatenate VOD segments: %v", err)
os.Exit(1)
}
} }
// youtube oauth flow // youtube oauth flow
@ -329,35 +339,39 @@ func main() {
log.Fatalf("Failed to save OAuth token: %v", err) log.Fatalf("Failed to save OAuth token: %v", err)
} }
// okay actually upload now! if (!dryRun) {
ytVideo, err := yt.UploadVideo(ctx, tokenSource, video, thumbnail, templates) // okay actually upload now!
if err != nil { ytVideo, err := yt.UploadVideo(ctx, tokenSource, video, thumbnail, templates)
log.Fatalf("Failed to upload video: %v", err)
os.Exit(1)
}
if verbose {
jsonString, err := json.MarshalIndent(ytVideo, "", " ")
if err != nil { if err != nil {
log.Fatalf("Failed to marshal video data json: %v", err) log.Fatalf("Failed to upload video: %v", err)
os.Exit(1)
} }
fmt.Println(string(jsonString)) if verbose {
} jsonString, err := json.MarshalIndent(ytVideo, "", " ")
log.Print("Video uploaded successfully!") if err != nil {
log.Fatalf("Failed to marshal video data json: %v", err)
}
fmt.Println(string(jsonString))
}
log.Print("Video uploaded successfully!")
// update metadata to reflect VOD is uploaded // update metadata to reflect VOD is uploaded
// TODO: rather than a boolean flag, link to actual video // TODO: rather than a boolean flag, link to actual video
metadata.Uploaded = true metadata.Uploaded = true
err = scanner.WriteMetadata(directory, metadata) err = scanner.WriteMetadata(directory, metadata)
if err != nil {
log.Fatalf("Failed to update metadata: %v", err)
}
// delete full VOD after upload, if requested
if deleteFullVod {
err = os.Remove(video.Filepath)
if err != nil { if err != nil {
log.Fatalf("Failed to delete full VOD: %v", err) log.Fatalf("Failed to update metadata: %v", err)
} }
// delete full VOD after upload, if requested
if deleteFullVod {
err = os.Remove(video.Filepath)
if err != nil {
log.Fatalf("Failed to delete full VOD: %v", err)
}
}
} else {
log.Print("Dry run: Skipping video upload")
} }
} }

View file

@ -8,6 +8,7 @@ OPTIONS:
--init: Initialise `directory` as a VOD directory. --init: Initialise `directory` as a VOD directory.
--logout: Logs out of the current YouTube account. --logout: Logs out of the current YouTube account.
-d, --deleteAfter: Deletes the full VOD after upload. -d, --deleteAfter: Deletes the full VOD after upload.
--dry-run: Don't upload or modify any files (Useful with -v)
-f, --force: Force uploading the VOD, even if it already exists. -f, --force: Force uploading the VOD, even if it already exists.
SOURCE: https://codeberg.org/arimelody/vodular SOURCE: https://codeberg.org/arimelody/vodular