Skip to content

Commit f0a6c01

Browse files
authored
Merge pull request #209 from porjo/localization
add video localizations
2 parents c8ae687 + c3630e9 commit f0a6c01

File tree

4 files changed

+55
-34
lines changed

4 files changed

+55
-34
lines changed

README.md

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,38 @@ Video title, description etc can specified via the command line flags or via a J
122122

123123
```json
124124
{
125-
"title": "my test title",
126-
"description": "my test description",
127-
"tags": ["test tag1", "test tag2"],
128-
"privacyStatus": "private",
129-
"madeForKids": false,
130-
"embeddable": true,
131-
"license": "creativeCommon",
132-
"publicStatsViewable": true,
133-
"publishAt": "2017-06-01T12:05:00+02:00",
134-
"categoryId": "10",
135-
"recordingDate": "2017-05-21",
136-
"playlistIds": ["xxxxxxxxxxxxxxxxxx", "yyyyyyyyyyyyyyyyyy"],
137-
"playlistTitles": ["my test playlist"],
138-
"language": "fr"
125+
"title": "my test title",
126+
"description": "my test description",
127+
"tags": [
128+
"test tag1",
129+
"test tag2"
130+
],
131+
"privacyStatus": "private",
132+
"madeForKids": false,
133+
"embeddable": true,
134+
"license": "creativeCommon",
135+
"publicStatsViewable": true,
136+
"publishAt": "2017-06-01T12:05:00+02:00",
137+
"categoryId": "10",
138+
"recordingDate": "2017-05-21",
139+
"playlistIds": [
140+
"xxxxxxxxxxxxxxxxxx",
141+
"yyyyyyyyyyyyyyyyyy"
142+
],
143+
"playlistTitles": [
144+
"my test playlist"
145+
],
146+
"language": "fr",
147+
"localizations": {
148+
"en": {
149+
"title": "My English Title",
150+
"description": "My English description"
151+
},
152+
"it": {
153+
"title": "Il mio titolo in italiano",
154+
"description": "La mia descrizione in italiano"
155+
}
156+
}
139157
}
140158
```
141159
- all fields are optional

files.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ type Date struct {
6969
time.Time
7070
}
7171

72-
func LoadVideoMeta(config Config, video *youtube.Video) (*VideoMeta, error) {
72+
func LoadVideoMeta(config Config) (*VideoMeta, *youtube.Video, error) {
7373
videoMeta := &VideoMeta{}
74+
video := &youtube.Video{}
7475

7576
video.Snippet = &youtube.VideoSnippet{}
7677
video.RecordingDetails = &youtube.VideoRecordingDetails{}
@@ -86,19 +87,21 @@ func LoadVideoMeta(config Config, video *youtube.Video) (*VideoMeta, error) {
8687
file, e := os.ReadFile(config.MetaJSON)
8788
if e != nil {
8889
e2 := fmt.Errorf("error reading file %q: %w", config.MetaJSON, e)
89-
return nil, e2
90+
return nil, nil, e2
9091
}
9192

9293
e = json.Unmarshal(file, &videoMeta)
9394
if e != nil {
9495
e2 := fmt.Errorf("error parsing file %q: %w", config.MetaJSON, e)
95-
return nil, e2
96+
return nil, nil, e2
9697
}
9798

9899
video.Snippet.Tags = videoMeta.Tags
99100
video.Snippet.Title = videoMeta.Title
100101
video.Snippet.Description = videoMeta.Description
101102
video.Snippet.CategoryId = videoMeta.CategoryId
103+
video.Localizations = videoMeta.Localizations
104+
102105
// Location has been deprecated by Google
103106
// see: https://developers.google.com/youtube/v3/revision_history#release_notes_06_01_2017
104107
/*
@@ -185,7 +188,7 @@ func LoadVideoMeta(config Config, video *youtube.Video) (*VideoMeta, error) {
185188
slices.Sort(playlistIDs)
186189
videoMeta.PlaylistIDs = slices.Compact(playlistIDs)
187190

188-
return videoMeta, nil
191+
return videoMeta, video, nil
189192
}
190193

191194
func Open(filename string, mediaType MediaType) (io.ReadCloser, int64, error) {

http.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ type VideoMeta struct {
4949

5050
// BCP-47 language code e.g. 'en','es'
5151
Language string `json:"language,omitempty"`
52+
53+
Localizations map[string]youtube.VideoLocalization `json:"localizations,omitempty"`
5254
}
5355

5456
func playlistList(service *youtube.Service, pageToken string) (*youtube.PlaylistListResponse, error) {

run.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ func Run(ctx context.Context, transport *limiter.LimitTransport, config Config,
9292
return fmt.Errorf("error building OAuth client: %w", err)
9393
}
9494

95-
upload := &youtube.Video{}
96-
97-
videoMeta, err := LoadVideoMeta(config, upload)
95+
videoMeta, uploadVideo, err := LoadVideoMeta(config)
9896
if err != nil {
9997
return fmt.Errorf("error loading video meta data: %w", err)
10098
}
@@ -111,28 +109,28 @@ func Run(ctx context.Context, transport *limiter.LimitTransport, config Config,
111109
}
112110

113111
var option googleapi.MediaOption
114-
var video *youtube.Video
112+
var resultVideo *youtube.Video
115113

116114
option = googleapi.ChunkSize(config.Chunksize)
117115

118-
call := service.Videos.Insert([]string{"snippet", "status", "recordingDetails"}, upload)
116+
call := service.Videos.Insert([]string{"snippet", "status", "localizations", "recordingDetails"}, uploadVideo)
119117
if config.SendFileName && config.Filename != "-" {
120118
filetitle := filepath.Base(config.Filename)
121119
slog.Debug("adding file name to request", "file", filetitle)
122120
call.Header().Set("Slug", filetitle)
123121
}
124-
video, err = call.NotifySubscribers(config.NotifySubscribers).Media(videoReader, option).Do()
122+
resultVideo, err = call.NotifySubscribers(config.NotifySubscribers).Media(videoReader, option).Do()
125123
if err != nil {
126-
if video != nil {
127-
return fmt.Errorf("error making YouTube API call: %w, %v", err, video.HTTPStatusCode)
124+
if resultVideo != nil {
125+
return fmt.Errorf("error making YouTube API call: %w, %v", err, resultVideo.HTTPStatusCode)
128126
} else {
129127
return fmt.Errorf("error making YouTube API call: %w", err)
130128
}
131129
}
132-
fmt.Printf("\nUpload successful! Video ID: %v\n", video.Id)
130+
fmt.Printf("\nUpload successful! Video ID: %v\n", resultVideo.Id)
133131

134132
if config.MetaJSONOut != "" {
135-
JSONOut, _ := json.Marshal(video)
133+
JSONOut, _ := json.Marshal(resultVideo)
136134
err = os.WriteFile(config.MetaJSONOut, JSONOut, 0666)
137135
if err != nil {
138136
return fmt.Errorf("error writing to video metadata file %q: %w", config.MetaJSONOut, err)
@@ -142,7 +140,7 @@ func Run(ctx context.Context, transport *limiter.LimitTransport, config Config,
142140

143141
if thumbReader != nil {
144142
fmt.Printf("Uploading thumbnail %q...\n", config.Thumbnail)
145-
_, err = service.Thumbnails.Set(video.Id).Media(thumbReader).Do()
143+
_, err = service.Thumbnails.Set(resultVideo.Id).Media(thumbReader).Do()
146144
if err != nil {
147145
return fmt.Errorf("error making YouTube API call: %w", err)
148146
}
@@ -154,7 +152,7 @@ func Run(ctx context.Context, transport *limiter.LimitTransport, config Config,
154152
captionObj := &youtube.Caption{
155153
Snippet: &youtube.CaptionSnippet{},
156154
}
157-
captionObj.Snippet.VideoId = video.Id
155+
captionObj.Snippet.VideoId = resultVideo.Id
158156
captionObj.Snippet.Language = config.Language
159157
captionObj.Snippet.Name = config.Language
160158
captionInsert := service.Captions.Insert([]string{"snippet"}, captionObj).Sync(true)
@@ -169,15 +167,15 @@ func Run(ctx context.Context, transport *limiter.LimitTransport, config Config,
169167
}
170168

171169
plx := &Playlistx{}
172-
if upload.Status.PrivacyStatus != "" {
173-
plx.PrivacyStatus = upload.Status.PrivacyStatus
170+
if uploadVideo.Status.PrivacyStatus != "" {
171+
plx.PrivacyStatus = uploadVideo.Status.PrivacyStatus
174172
}
175173

176174
if len(videoMeta.PlaylistIDs) > 0 {
177175
plx.Title = ""
178176
for _, pid := range videoMeta.PlaylistIDs {
179177
plx.Id = pid
180-
err = plx.AddVideoToPlaylist(service, video.Id)
178+
err = plx.AddVideoToPlaylist(service, resultVideo.Id)
181179
if err != nil {
182180
return fmt.Errorf("error adding video to playlist: %w", err)
183181
}
@@ -188,7 +186,7 @@ func Run(ctx context.Context, transport *limiter.LimitTransport, config Config,
188186
plx.Id = ""
189187
for _, title := range videoMeta.PlaylistTitles {
190188
plx.Title = title
191-
err = plx.AddVideoToPlaylist(service, video.Id)
189+
err = plx.AddVideoToPlaylist(service, resultVideo.Id)
192190
if err != nil {
193191
return fmt.Errorf("error adding video to playlist: %w", err)
194192
}

0 commit comments

Comments
 (0)