kkdai / youtube

Download Youtube Video in Golang
MIT License
3.3k stars 430 forks source link

.downloadChunk > invalid memory address or nil pointer dereference #294

Closed ramensp closed 1 year ago

ramensp commented 1 year ago

Hi, I get the error "invalid memory address or nil pointer dereference" when I want to download a video with a length of 11:04 minutes.

Version: go1.20.6 linux/amd64

Distributor ID: Ubuntu
Description:    Ubuntu 22.04.2 LTS
Release:        22.04
Codename:       jammy

This is the error message:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0xc902ac]

goroutine 6192 [running]:
github.com/kkdai/youtube/v2.(*Client).downloadChunk(0xc011fb9b00?, 0xc011fb9c00, {0xc00003e0c8?, 0x0?, 0x58f1c33631cd5c72?})
        /home/jake/go/pkg/mod/github.com/kkdai/youtube/v2@v2.8.1/client.go:626 +0x36c
github.com/kkdai/youtube/v2.(*Client).downloadChunked.func1()
        /home/jake/go/pkg/mod/github.com/kkdai/youtube/v2@v2.8.1/client.go:437 +0x1ed
created by github.com/kkdai/youtube/v2.(*Client).downloadChunked
        /home/jake/go/pkg/mod/github.com/kkdai/youtube/v2@v2.8.1/client.go:424 +0x33b

This is my code:

config.go:

type Config struct {
    Debug       bool
    MaxRoutines int
    ChunkSize   int
}

var defaultConfig = Config{
    Debug:       false,
    MaxRoutines: 10,
    ChunkSize:   10,
}

init.go:

package youtube

import (
    "github.com/J2eBD/YoDo/config"
    yt "github.com/kkdai/youtube/v2"
)

var client yt.Client

func Init() {
    client = yt.Client{
        Debug:       config.Store.Debug,
        MaxRoutines: config.Store.MaxRoutines,
        ChunkSize:   int64(config.Store.ChunkSize * 1024 * 1024),
    }
}

I initialize the client via this function, because I want to apply changes directly

download.go:

func Download(id, dir string, audioOnly bool) error {
    video, err := client.GetVideo(id)
    if err != nil {
        return err
    }

    video.Formats.WithAudioChannels()
    video.Formats.Sort()

    var format yt.Format
    fileExt := "mp4"
    if audioOnly {
        for _, f := range video.Formats {
            if f.AudioChannels != 0 {
                format = f
                fileExt = "mp3"
                break
            }
        }
    } else {
        format = video.Formats[0]
    }

    reader, _, err := client.GetStream(video, &format)
    if err != nil {
        return err
    }
    defer reader.Close()

    filePath := fmt.Sprintf("%s.%s", path.Join(dir, video.Title), fileExt)
    file, err := os.Create(filePath)
    if err != nil {
        return err
    }
    defer file.Close()

    _, err = io.Copy(file, reader)
    if err != nil {
        return err
    }

    return nil
}
corny commented 1 year ago

I've just downloaded a video of 14 minutes without any isuses. Please try again with the latest release. Does this issue still exist?