func showVideoInfo(id string) { i := info.NewInfo(id) fmt.Println("fetching video info") err := i.Fetch() if err != nil { fmt.Println("there was an error while fetching info for", id) } fmt.Println("available itags:") fmt.Println("") for _, stream := range i.Streams() { fmt.Printf(" [%d] %s - %s\n", stream.Format.Itag, stream.Format.Container, stream.Format.Video.Resolution) } }
func main() { flag.IntVar(&inputItag, "itag", -1, "itag of the stream to download") flag.Usage = func() { printUsage() } flag.Parse() if flag.NArg() == 0 { printUsage() os.Exit(0) } cmd := flag.Arg(0) switch cmd { case "get": if flag.NArg() < 2 { printUsageCommand("get") os.Exit(1) } id := flag.Arg(1) i := info.NewInfo(id) err := i.Fetch() if err != nil { fmt.Println("there was an error fetching info for the video", id) os.Exit(1) } var downloadStream *info.Stream if inputItag != -1 { // see if we can satisfy the user for _, stream := range i.Streams() { if stream.Format.Itag == inputItag { downloadStream = stream break } } if downloadStream == nil { fmt.Printf("%s doesn't have a stream with format with itag %d. Picking default stream (the best one available).\n", id, inputItag) } } // pick the best stream by default if downloadStream == nil { downloadStream = i.Streams()[0] } var output io.WriteCloser if flag.NArg() > 2 { if flag.Arg(2) == "-" { output = os.Stdout } else { fmt.Println("writing download result to:", flag.Arg(2)) output, err = os.Create(flag.Arg(2)) if err != nil { fmt.Println("failed to create file") os.Exit(1) } } } else { ext, err := downloadStream.Format.Extension() if err != nil { // no extension, I don't have all possibilities listed in (*Format).Extension() ext = "" } fmt.Println("writing download result to:", i.Id+ext) output, err = os.Create(i.Id + ext) if err != nil { fmt.Println("failed to create file") os.Exit(1) } } download.Download(i, downloadStream, output) output.Close() case "info": if flag.NArg() < 2 { printUsageCommand("info") os.Exit(1) } id := flag.Arg(1) showVideoInfo(id) case "help": if flag.NArg() == 2 { printUsageCommand(flag.Arg(1)) } else { printUsage() } default: printUsage() } }