2

This is just a sample code, I was using range_func without any problems and then youtube links somehow starts from the the beginning of the video no matter what start time I gave. Odd thing is it was working perfect and somehow it's only gets end time but start is always 0. I tried with other platforms but they were fine just youtube does this. Any ideas or alternatives? (ffmpeg -ss is not an alternative since it's already downloading the whole video and then cuts it so it's useless.)

from yt_dlp import YoutubeDL
from yt_dlp.utils import download_range_func

vlink = input('paste link: ')
start = int(input('start time: '))
end = start + 6
ydl_opts = {
    'format': 'bestaudio/best',
            'download_ranges': download_range_func(None, [(start, end)]),
            'outtmpl': 'output.%(ext)s', 
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'wav',
            }],
            'postprocessor_args': [
                '-ar', '44100',
                '-ac', '1',
                '-acodec', 'pcm_s16le',
            ],
            'prefer_ffmpeg': True
}
with YoutubeDL(ydl_opts) as ydl:
    ydl.download(vlink)

Well I added two args(explained with comments) and problem solved.

from yt_dlp import YoutubeDL
from yt_dlp.utils import download_range_func

vlink = input('paste link: ')
start = int(input('start time: '))
end = start + 6
ydl_opts = {
    'format': 'bestaudio/best',
            'download_ranges': download_range_func(None, [(start, end)]),
            'force_keyframes_at_cuts': True, # for yt links
            'outtmpl': 'output.%(ext)s', 
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'wav',
            }],
            'postprocessor_args': [
                '-ar', '44100',
                '-ac', '1',
                '-acodec', 'pcm_s16le',
                '-f', 'WAV', #for other platforms which uses .m4a
            ],
            'prefer_ffmpeg': True
}
with YoutubeDL(ydl_opts) as ydl:
    ydl.download(vlink)
wutwut
  • 33
  • 7
  • maybe YouTube changed something on its page and now it doesn't work. And it may need to wait for newer version. And maybe at this moment `ffmpeg -ss` is the only alternative. – furas Oct 01 '22 at 21:55
  • I already thought of it since it's just happened from nowhere. if that's the case best thing is just open an issue at github thanks for the reply – wutwut Oct 01 '22 at 22:03
  • maybe first check existing issues in github - maybe someone had similar problem. – furas Oct 01 '22 at 22:10
  • already checked but I ended up asking in discord server, waiting for info update. – wutwut Oct 01 '22 at 23:00

1 Answers1

1

For anyone else who comes across this, I wanted to note that the comment about ffmpeg -ss being useless is correct as stated, but incomplete—I recently had a problem similar to this myself. I didn't run across download_ranges in my searches, but if you use ffmpeg as an external downloader and pass ffmpeg_i options as external downloader args, these are added before ffmpeg's -i flag, causing the seek to be factored into the download rather than performed afterward. Something like this:

start = 4
end = start + 6

ydl_opts = {
  "external_downloader": "ffmpeg",
  "external_downloader_args": {"ffmpeg_i": ["-ss", str(start), "-to", str(end)]},
  # ...
}

For more complete info, I put together a gist about the process I ended up using.

Josh
  • 1,563
  • 11
  • 16