automatically upload nearby thumbnail.png

This commit is contained in:
ari melody 2026-06-23 18:28:50 +01:00
parent fc1d6b20c1
commit 2fcbc31f77
Signed by: ari
GPG key ID: 60B5F0386E3DDB7E
4 changed files with 75 additions and 19 deletions

View file

@ -41,7 +41,12 @@ type (
Part int
Date time.Time
Tags []string
Filename string
Filepath string
SizeBytes int64
}
Thumbnail struct {
Filepath string
SizeBytes int64
}
)
@ -65,7 +70,7 @@ func BuildVideo(metadata *scanner.Metadata) (*Video, error) {
Part: metadata.Part,
Date: metadata.Date.AsTime(time.UTC),
Tags: metadata.Tags,
Filename: path.Join(
Filepath: path.Join(
metadata.FootageDir,
fmt.Sprintf(
"%s-fullvod.mkv",
@ -216,6 +221,7 @@ func UploadVideo(
ctx context.Context,
tokenSource oauth2.TokenSource,
video *Video,
thumbnail *Thumbnail,
templates *Template,
) (*youtube.Video, error) {
title, err := BuildTemplate(video, templates.Title)
@ -233,6 +239,8 @@ func UploadVideo(
return nil, err
}
// upload video
videoService := youtube.NewVideosService(service)
categoryId := YT_CATEGORY_ENTERTAINMENT
@ -242,7 +250,7 @@ func UploadVideo(
}
}
call := videoService.Insert([]string{
videoInsertCall := videoService.Insert([]string{
"snippet", "status",
}, &youtube.Video{
Snippet: &youtube.VideoSnippet{
@ -256,26 +264,49 @@ func UploadVideo(
},
}).NotifySubscribers(false)
file, err := os.Open(video.Filename)
videoFile, err := os.Open(video.Filepath)
if err != nil {
log.Fatalf("Failed to open file: %v\n", err)
log.Fatalf("Failed to open video: %v\n", err)
return nil, err
}
call.Media(file)
videoInsertCall.Media(videoFile)
log.Println("Uploading video...")
call.ProgressUpdater(func(current, total int64) {
videoInsertCall.ProgressUpdater(func(current, total int64) {
// for some reason, this only returns 0.
// instead, we pull the file size from the ffmpeg output directly.
if total == 0 { total = video.SizeBytes }
fmt.Printf("Uploading... (%.2f%%)\n", float64(current) / float64(total) * 100)
fmt.Printf("\t(%.2f%%)\n", float64(current) / float64(total) * 100)
})
ytVideo, err := call.Do()
ytVideo, err := videoInsertCall.Do()
if err != nil {
log.Fatalf("Failed to upload video: %v\n", err)
return nil, err
}
// upload thumbnail
thumbnailService := youtube.NewThumbnailsService(service)
thumbnailSetCall := thumbnailService.Set(ytVideo.Id)
thumbnailFile, err := os.Open(thumbnail.Filepath)
if err != nil {
log.Fatalf("Failed to open thumbnail: %v\n", err)
return nil, err
}
thumbnailSetCall.Media(thumbnailFile)
log.Println("Uploading thumbnail...")
thumbnailSetCall.ProgressUpdater(func(current, total int64) {
if total == 0 { total = thumbnail.SizeBytes }
fmt.Printf("\t(%.2f%%)\n", float64(current) / float64(total) * 100)
})
// kinda don't care about the response here- so long as it works!
_, err = thumbnailSetCall.Do()
if err != nil {
log.Printf("Failed to upload thumbnail: %v\n", err)
}
return ytVideo, err
}