Skip to content

Commit c2d27ec

Browse files
committed
download cmd handle playlist & video
1 parent 0f9b905 commit c2d27ec

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (c *Client) GetPlaylist(url string) (*Playlist, error) {
7272
// for these videos. Playlist entries cannot be downloaded, as they lack all the required metadata, but
7373
// can be used to enumerate all IDs, Authors, Titles, etc.
7474
func (c *Client) GetPlaylistContext(ctx context.Context, url string) (*Playlist, error) {
75-
id, err := extractPlaylistID(url)
75+
id, err := ExtractPlaylistID(url)
7676
if err != nil {
7777
return nil, fmt.Errorf("extractPlaylistID failed: %w", err)
7878
}

cmd/youtubedr/download.go

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,57 @@ import (
77
"os/exec"
88
"strings"
99

10+
"github.com/kkdai/youtube/v2"
1011
"github.com/spf13/cobra"
1112
)
1213

14+
var (
15+
errNotID = fmt.Errorf("cannot detect ID in given input")
16+
)
17+
1318
// downloadCmd represents the download command
1419
var downloadCmd = &cobra.Command{
1520
Use: "download",
16-
Short: "Downloads a video from youtube",
21+
Short: "Downloads a video or a playlist from youtube",
1722
Example: `youtubedr -o "Campaign Diary".mp4 https://www.youtube.com/watch\?v\=XbNghLqsVwU`,
1823
Args: cobra.ExactArgs(1),
1924
Run: func(cmd *cobra.Command, args []string) {
20-
exitOnError(download(args[0]))
25+
if playlistID, err := youtube.ExtractPlaylistID(args[0]); err != nil {
26+
if videoID, err := youtube.ExtractVideoID(args[0]); err != nil {
27+
exitOnError(errNotID)
28+
} else {
29+
log.Printf(
30+
"download video %s to directory %s\n",
31+
videoID,
32+
outputDir,
33+
)
34+
exitOnError(download(videoID))
35+
}
36+
} else {
37+
if playlist, err := getDownloader().GetPlaylist(playlistID); err != nil {
38+
exitOnError(err)
39+
} else {
40+
log.Printf(
41+
"download %d videos from playlist %s to directory %s\n",
42+
len(playlist.Videos),
43+
playlist.ID,
44+
outputDir,
45+
)
46+
var errors []error
47+
outputFileOrigin := outputFile
48+
for i, v := range playlist.Videos {
49+
if len(outputFileOrigin) != 0 {
50+
outputFile = fmt.Sprintf("%s-%d", outputFile, i)
51+
}
52+
if err := download(v.ID); err != nil {
53+
errors = append(errors, err)
54+
}
55+
}
56+
if len(errors) > 0 {
57+
exitOnErrors(errors)
58+
}
59+
}
60+
}
2161
},
2262
}
2363

@@ -30,7 +70,7 @@ var (
3070
func init() {
3171
rootCmd.AddCommand(downloadCmd)
3272

33-
downloadCmd.Flags().StringVarP(&outputFile, "filename", "o", "", "The output file, the default is genated by the video title.")
73+
downloadCmd.Flags().StringVarP(&outputFile, "filename", "o", "", "The output file, the default is generated from the video title.")
3474
downloadCmd.Flags().StringVarP(&outputDir, "directory", "d", ".", "The output directory.")
3575
addQualityFlag(downloadCmd.Flags())
3676
addMimeTypeFlag(downloadCmd.Flags())
@@ -42,8 +82,6 @@ func download(id string) error {
4282
return err
4383
}
4484

45-
log.Println("download to directory", outputDir)
46-
4785
if strings.HasPrefix(outputQuality, "hd") {
4886
if err := checkFFMPEG(); err != nil {
4987
return err

cmd/youtubedr/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ func exitOnError(err error) {
1515
os.Exit(1)
1616
}
1717
}
18+
19+
func exitOnErrors(errors []error) {
20+
if len(errors) != 0 {
21+
for _, err := range errors {
22+
fmt.Fprintln(os.Stderr, err)
23+
}
24+
os.Exit(1)
25+
}
26+
}

playlist.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type PlaylistEntry struct {
3939
Duration time.Duration
4040
}
4141

42-
func extractPlaylistID(url string) (string, error) {
42+
func ExtractPlaylistID(url string) (string, error) {
4343
if playlistIDRegex.Match([]byte(url)) {
4444
return url, nil
4545
}

0 commit comments

Comments
 (0)