Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retry when connection disconnect not working

Tags:

youtube-dl

I am using youtube-dl for downloading the videos from YouTube. But in my office the internet will disconnect every 20Mb of download. [Error: Connection forcibly closed by remote server].

I have to type the URL again to resume the download and again it will disconnect after '20Mb' I want youtube-dl to reconnect and retry to download the file.

I tried using --retries switch but it is not retrying once disconnected.

Is there any inbuild method or Work around for this?

like image 331
Olivarsham Avatar asked Apr 04 '16 11:04

Olivarsham


2 Answers

Get a bash , either via steve's win-bash, the new windows10/Ubuntu thing or cygwin

Call youtube-dl like this:

while ! youtube-dl <video_uri> -c --socket-timeout 5; do echo DISCONNECTED; done

You may want to add some sleep time between retries.

while ! youtube-dl <video_uri> -c --socket-timeout 5; do echo DISCONNECTED; sleep 5; done

There should be a power shell equivalent, or an ugly batch while loop checking ERRORLEVEL

like image 58
xvan Avatar answered Oct 20 '22 05:10

xvan


Educated guess

My best guess would be to specify a cache directory, and use the -c flag to force it to continue downloads if possible.

Source: youtube-dl man page

--cache-dir DIR
              Location  in  the  filesystem  where  youtube-dl  can  store  some  downloaded  information  permanently.   By  default
              $XDG_CACHE_HOME  /youtube-dl or ~/.cache/youtube-dl .  At the moment, only YouTube player files (for videos with obfus‐
              cated signatures) are cached, but that may change.

-c, --continue
              Force resume of partially downloaded files.  By default, youtube-dl will resume downloads if possible.

Alternative solution

If you want to give python a try, this script should do what you need with some minor tweaking.

import sys
import youtube_dl

def download_no_matter_what(url):
    try:
        youtube_dl.YoutubeDL(options).download([url])
    except OSError:
        download_no_matter_what(url)
    except KeyboardInterrupt:
        sys.exit()

if __name__ == '__main__':
    # Read the URL from the command line
    url = sys.argv[1]

    # Specify extra command line options here
    options = {} 

    # GET THAT VIDEO! 
    download_no_matter_what(url)

Reference for the youtube_dl API: https://github.com/rg3/youtube-dl/blob/master/README.md#readme

like image 44
nathanshimp Avatar answered Oct 20 '22 03:10

nathanshimp