kkdai / youtube

Download Youtube Video in Golang
MIT License
3.37k stars 433 forks source link

No Audio When Downloading Programatically #206

Closed ghost closed 3 years ago

ghost commented 3 years ago

Hello,

I'm using this code to download videos:

        client := youtube.Client{}

    video, err := client.GetVideo(videoID)
    if err != nil {
        panic(err)
    }

    stream, _, err := client.GetStream(video, &video.Formats[0])
    if err != nil {
        panic(err)
    }

    file, err := os.Create("video.mp4")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    _, err = io.Copy(file, stream)
    if err != nil {
        panic(err)
    }

I'm using the example code.

The video downloads but there is no audio.

Possibly related to #204

nksapphire commented 3 years ago

The quickest workaround is to change line: https://github.com/nksapphire/youtube/blob/3650856f77bfc076817ef793bbd3ef2760016e96/format_list.go#L14 to if (list[i].Quality == quality || list[i].QualityLabel == quality) && list[i].AudioChannels > 0

There are a list of formats available, and we need to pick up a format with audio channels to download. Or else there will be no audio.

Eventually, a logic to filter out formats without audio channels should be applied in this function: https://github.com/kkdai/youtube/blob/3650856f77bfc076817ef793bbd3ef2760016e96/cmd/youtubedr/downloader.go#L97

nksapphire commented 3 years ago

Created a PR for formal solution: https://github.com/kkdai/youtube/pull/207

Julian-Chu commented 3 years ago

I have merged the @nksapphire 's bugfix. It should works with newest version.

rodrigo-brito commented 3 years ago

In the last release, is it working?

Julian-Chu commented 3 years ago

@rodrigo-brito yes, it works in version 2.7.3

prologic commented 3 years ago

Even with #207 merged and v2.7.4 I can stillĀ reproduce this problem.

rodrigo-brito commented 3 years ago

Hi @prologic, I solved it using WithAudioChannels() filter.

    video, err := client.GetVideo(youtubeID)
    if err != nil {
        return "", err
    }

    formats := video.Formats.WithAudioChannels()
    stream, _, err := client.GetStream(video, &formats[0])
    if err != nil {
        return "", err
    }
rodrigo-brito commented 3 years ago

It is not documented, I discovered in the Pull Request linked https://github.com/kkdai/youtube/pull/207

prologic commented 3 years ago

Nice! Thanks.

Julian-Chu commented 3 years ago

Hi @prologic, I solved it using WithAudioChannels() filter.

  video, err := client.GetVideo(youtubeID)
  if err != nil {
      return "", err
  }

  formats := video.Formats.WithAudioChannels()
  stream, _, err := client.GetStream(video, &formats[0])
  if err != nil {
      return "", err
  }

@rodrigo-brito how about making a PR for the example code šŸš€

Julian-Chu commented 3 years ago

updated example_test. Close issue.