ytsync/local/ytdlVideoSource.go

73 lines
1.6 KiB
Go
Raw Normal View History

package local
import (
log "github.com/sirupsen/logrus"
2021-11-04 21:28:28 +01:00
"github.com/lbryio/ytsync/v5/downloader/ytdl"
)
type YtdlVideoSource struct {
downloader Ytdl
2021-11-04 21:28:28 +01:00
enrichers []YouTubeVideoEnricher
}
2021-11-04 21:28:28 +01:00
func NewYtdlVideoSource(downloadDir string, config *YouTubeSourceConfig) (*YtdlVideoSource, error) {
ytdl, err := NewYtdl(downloadDir)
if err != nil {
return nil, err
}
source := YtdlVideoSource {
downloader: *ytdl,
}
2021-11-04 21:28:28 +01:00
if config.YouTubeAPIKey != "" {
ytapiEnricher := NewYouTubeAPIVideoEnricher(config.YouTubeAPIKey)
source.enrichers = append(source.enrichers, ytapiEnricher)
}
return &source, nil
}
func (s *YtdlVideoSource) GetVideo(id string) (*SourceVideo, error) {
metadata, err := s.downloader.GetVideoMetadata(id)
if err != nil {
return nil, err
}
videoPath, err := s.downloader.GetVideoFile(id)
if err != nil {
return nil, err
}
2021-11-04 21:28:28 +01:00
var bestThumbnail *ytdl.Thumbnail = nil
for i, thumbnail := range metadata.Thumbnails {
if i == 0 || bestThumbnail.Width < thumbnail.Width {
bestThumbnail = &thumbnail
}
}
sourceVideo := SourceVideo {
ID: id,
Title: &metadata.Title,
Description: &metadata.Description,
SourceURL: "\nhttps://www.youtube.com/watch?v=" + id,
Languages: []string{},
Tags: metadata.Tags,
2021-11-04 21:28:28 +01:00
ReleaseTime: nil,
ThumbnailURL: &bestThumbnail.URL,
FullLocalPath: videoPath,
}
2021-11-04 21:28:28 +01:00
for _, enricher := range s.enrichers {
err = enricher.EnrichMissing(&sourceVideo)
if err != nil {
log.Warnf("Error enriching video %s, continuing enrichment: %v", id, err)
}
}
log.Debugf("Source video retrieved via ytdl: %v", sourceVideo)
return &sourceVideo, nil
}