Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Nov 5, 2020. It is now read-only.
/ytdlPublic archive

A CLI to download YouTube videos on the go.

License

NotificationsYou must be signed in to change notification settings

ytdl-node/ytdl

Repository files navigation

ytdl-buildytdl-releaseytdl-publish

ytdl provides a library to integrate a Youtube Downloader forNode.js projects, and a CLI to download content fromYoutube.ytdl can also stream audio/video from YouTube,without downloading, directly to your locally installed media player!

Note: You needffmpeg to be installed on your computer for complete functionality. Without it, you won't be able to some formats/qualities of videos. If ffmpeg is absent, ytdl will prompt you for installation, it is recommended that you install it.


Note: To use the inbuilt mp3 playerytdl-mp3 on Linux distros,<alsa/asoundlib.h> header file must be present. The preinstall script logs a warning if it is not present. Ubuntu/Debian users require to install thelibasound2-dev package (sudo apt-get install libasound2-dev), if not present already; before installing the library.

Contents

Installation

Library

Via npm

npm install @ytdl/ytdl

CLI

Via npm (recommended)

Note:DO NOT use sudo to install global packages! The correct way to do it is to tell npm where to install it's global packages:npm config set prefix ~/.local. Make sure~/.local/bin is added toPATH.

npm install @ytdl/ytdl -g

Via curl

  • As a single file (from latest Github Release):
# Needs both curl and wgetsh -c"$(curl -fsSL https://raw.githubusercontent.com/ytdl-node/ytdl/master/bin/install-latest)"
  • From GitHub repository:
sh -c"$(curl -fsSL https://raw.githubusercontent.com/ytdl-node/ytdl/master/bin/install)"

Via wget

  • As a single file (from latest Github Release):
# Needs both curl and wgetsh -c"$(wget -O- https://raw.githubusercontent.com/ytdl-node/ytdl/master/bin/install-latest)"
  • From GitHub repository:
sh -c"$(wget -O- https://raw.githubusercontent.com/ytdl-node/ytdl/master/bin/install)"

Via commands

git clone https://github.com/ytdl-node/ytdl.gitcd ytdlnpm install./bin/ytdl -hechoecho"Add$(pwd)/bin to PATH"

API

Example

constytdl=require('@ytdl/ytdl');asyncfunctionvideoDownloader(){constvideo=awaitytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ');awaitvideo.download('360p','ytdl.mp4');}videoDownloader();

OR

// Without using async-await.constytdl=require('@ytdl/ytdl');ytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ').then((video)=>{video.download('360p','ytdl.mp4');});

Brief

The functionytdl.init(link: string) orytdl.default(link: string) returns a Promise which resolves with an object of typeYtdl.

AYtdl object has the following properties:

  • info.videoId:string, stores the video ID.
  • info.videoTitle:string, stores the title of the video.
  • info.videoTime:string, stores the time of the video.
  • info.videoDescription:string, stores the description of the video.
  • info.size(quality[, options]):Number, stores the size of the stream inbytes.
  • info.all():Object, returns an object consisting of id, title, time, description.
  • download(quality, filename[, options]):Promise<void>, downloads the video/audio from YouTube.
  • setLogLevel(level):void, allows you to enable or disable logging depending on the level.
  • downloadByItag(itag, filename):Promise<void>, downloads from YouTube using the itag property.
  • stream(quality[, options, headers]):Promise<any>, returns aNode.js stream.

Any reference tovideo refers to an object returned byytdl.default('link').

options: object

  • audioOnly: true | false
  • videoOnly: true | false
// Exampleconstoptions={audioOnly:true,videoOnly:false};

quality: string

  • For audio: low, medium, high, any.
  • For video: 144p, 360p, 480p, 720p, 1080p.
// Exampleconstquality='low';

video.download(quality, path[, options])

constytdl=require('@ytdl/ytdl');asyncfunctionvideoDownloader(){constvideo=awaitytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ');awaitvideo.download('360p','ytdl.mp4');}videoDownloader();

OR

// Without using async-await.constytdl=require('@ytdl/ytdl');ytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ').then((video)=>{video.download('360p','ytdl.mp4');});
  • This function first searches for a stream which has both audio and video in it. If it doesn't find it, it will search for a separate audio and video stream and combine it using ffmpeg.

Currentlyytdl uses thefluent-ffmpeg package which requires ffmpeg to be installed on the computer.

options:

  • audioOnly: true | false
  • videoOnly: true | false

audioOnly:

constytdl=require('@ytdl/ytdl');asyncfunctionvideoDownloader(){constvideo=awaitytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ');awaitvideo.download('medium','audio.mp3',{audioOnly:true});// quality: 'low', 'medium', 'high', 'any'}videoDownloader();

videoOnly:

Note: There will be no sound.

constytdl=require('@ytdl/ytdl');asyncfunctionvideoDownloader(){constvideo=awaitytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ');awaitvideo.download('720p','video.mp4',{videoOnly:true});// quality: '144p', '360p', '480p', '720p', '1080p'}videoDownloader();

video.downloadByItag(url, itag, path)

constytdl=require('@ytdl/ytdl');asyncfunctionvideoDownloader(){constvideo=awaitytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ');awaitvideo.downloadByItag(396,'ytdl.mp4');}videoDownloader();

OR

// Without using async-await.constytdl=require('@ytdl/ytdl');ytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ').then((video)=>{video.downloadByItag(396,'ytdl.mp4');});

video.stream(quality[, options, headers])

  • This function returns a Node.js stream.
  • This may be piped tofs.createWriteStream(filename) to save the stream into a file.

Note: The download function merges separate audio-only and video-only stream when a combined stream is unavailable. This function however will return the appropriate stream if and only if it is available. You may require to pass options, having propertiesaudioOnly andvideoOnly to get the desired stream. E.G.video.stream('480p', { videoOnly: true }).

constytdl=require('@ytdl/ytdl');constfs=require('fs');asyncfunctiondownload(){constvideo=awaitytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ');conststream=awaitvideo.stream('360p');// The variable stream now holds a Node.js stream.// Sample use of stream is as follows:stream.pipe(fs.createWriteStream('ytdl.mp4')).on('finish',(err)=>{if(err)console.log(err);elseconsole.log('Stream saved successfully.');});}download();

OR

// Without using async-await.constytdl=require('@ytdl/ytdl');constfs=require('fs');ytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ').then((video)=>{video.stream('360p').then((stream)=>{// The variable stream now holds a Node.js stream.// Sample use of stream is as follows:stream.pipe(fs.createWriteStream('ytdl.mp4')).on('finish',(err)=>{if(err)console.log(err);elseconsole.log('Stream saved successfully.');});});});

video.streamByItag(itag[, headers])

constytdl=require('@ytdl/ytdl');constfs=require('fs');asyncfunctiondownload(){constvideo=awaitytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ');conststream=awaitvideo.stream(18);// The variable stream now holds a Node.js stream.// Sample use of stream is as follows:stream.pipe(fs.createWriteStream('ytdl.mp4')).on('finish',(err)=>{if(err)console.log(err);elseconsole.log('Stream saved successfully.');});}download();

OR

// Without using async-await.constytdl=require('@ytdl/ytdl');constfs=require('fs');ytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ').then((video)=>{video.streamByItag(18).then((stream)=>{// The variable stream now holds a Node.js stream.// Sample use of stream is as follows:stream.pipe(fs.createWriteStream('ytdl.mp4')).on('finish',(err)=>{if(err)console.log(err);elseconsole.log('Stream saved successfully.');});});});

video.play(quality[, options, player])

  • Play audio or video from YouTube in your local media player.
  • quality may be anitag or among the ones mentionedhere.
  • Options are ofthis format. This is ignored if parameterquality is anitag.
  • The function returns aPlayer object, with attributesplayer andplay(url, stream).

Audio

  • By default, audio is played on a cross-platform player integrated with ytdl.
  • However, if the parameterplayer is passed, it is played on the specified media player.

Video

  • cvlc is the default video player.
  • Allowed media players are:cvlc,vlc,mplayer,afplay,mpg123,mpg321,play,omxplayer,aplay,cmdmp3.

The media player set must be on yourPATH or in your Environment Variables. On UNIX based systems, you can check if your media player is on your PATH by using the which command, e.g.which mplayer.

constytdl=require('@ytdl/ytdl');asyncfunctionplay(){constvideo=awaitytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ');video.play('any',{audioOnly:true});// Play audio of any quality on ytdl-mp3 player.}play();

OR

constytdl=require('@ytdl/ytdl');ytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ').then((video)=>{video.play('any',{audioOnly:true});// Play audio of any quality on ytdl-mp3 player.});
  • ThePlayer object returned can be used to log the name of the player being used.
  • In this example,mplayer is being used.
asyncfunctionplayLocal(){constvideo=awaitytdl.init('https://www.youtube.com/watch?v=A7ry4cx6HfY');constplayer=awaitvideo.play('any',{audioOnly:true},'mplayer');console.log(`Player:${player.player}`);// Logs the player on which media is being played.}playLocal();

OR

ytdl.init('https://www.youtube.com/watch?v=A7ry4cx6HfY').then((video)=>{video.play('any',{audioOnly:true},'mplayer').then((player)=>{console.log(`Player:${player.player}`);// Logs the player on which media is being played.});});

video.setLogLevel(level)

  • level can be one of the following:
    • error
    • warn
    • info
    • http
    • verbose
    • debug
    • silly
  • A level lower in the list also enables the levels before it.
  • E.G., a log level of debug will also enableverbose, http, info, warn, error.
constytdl=require('@ytdl/ytdl');asyncfunctionvideoDownloader(){constvideo=awaitytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ');// This line will enable logging info to console while downloading content.video.setLogLevel('info');awaitvideo.downloadByItag(396,'ytdl.mp4');}videoDownloader();

video.info.size(quality|itag[, options])

  • Returns size in bytes.
  • A number is treated as anitag whereas a string is treated asquality.
  • Options may be passed only withquality, else it will be ignored.
constytdl=require('@ytdl/ytdl');asyncfunctionvideoSize(){constvideo=awaitytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ');constsize=video.info.size('360p');// can pass options: { audioOnly: boolean, videoOnly: boolean }constsizeItag=video.info.size(396);console.log(`Video Size for 360p:${Math.round(size/(1024*1024))}M`);console.log(`Video Size for itag = 396:${Math.round(sizeItag/(1024*1024))}M`);}videoSize();

OR

constytdl=require('@ytdl/ytdl');ytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ').then((video)=>{constsize=video.info.size('360p');// can pass options: { audioOnly: boolean, videoOnly: boolean}constsizeItag=video.info.size(396);console.log(`Video Size for 360p:${Math.round(size/(1024*1024))}M`);console.log(`Video Size for itag = 396:${Math.round(sizeItag/(1024*1024))}M`);});

video.info.all()

constytdl=require('@ytdl/ytdl');asyncfunctionvideoInfo(){constvideo=awaitytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ');const{ id, title, time, description}=video.info.all();console.log(`Video Title:${title}`);}videoInfo();

OR

// Without using async-await.constytdl=require('@ytdl/ytdl');ytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ').then((video)=>{const{ id, title, time, description}=video.info.all();console.log(`Video Title:${title}`);});

video.info.fetchFormatData(quality[, options])

  • Returns an object:{ url: string, fmt: object }.
  • url holds the download URL for the video.
  • fmt holds other data about the format such ascontentLength,fps,mimeType, etc.
  • options are same as invideo.download.
constytdl=require('@ytdl/ytdl');asyncfunctiongetData(){constvideo=awaitytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ');const{ url, fmt}=video.info.fetchFormatData('360p');console.log(`Download url:${url}`);console.log(`Format data:${fmt}`);}getData();

OR

ytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ').then((video)=>{const{ url, fmt}=video.info.fetchFormatData('360p');console.log(`Download url:${url}`);console.log(`Format data:${fmt}`);});

video.info.fetchFormatDataByItag(itag)

  • Same asvideo.info.fetchFormatData(quality); except, it fetches data by itag instead of quality.
constytdl=require('@ytdl/ytdl');asyncfunctiongetData(){constvideo=awaitytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ');const{ url, fmt}=video.info.fetchFormatDataByItag(18);console.log(`Download url:${url}`);console.log(`Format data:${fmt}`);}getData();

OR

ytdl.init('https://www.youtube.com/watch?v=fJ9rUzIMcZQ').then((video)=>{const{ url, fmt}=video.info.fetchFormatDataByItag(18);console.log(`Download url:${url}`);console.log(`Format data:${fmt}`);});

ytdl.fromName(name)

  • Create aYtdl object using the searched name.
constytdl=require('..');asyncfunctionplay(){constvideo=awaitytdl.fromName('Another Day');video.play('any');}play();

OR

constytdl=require('..');ytdl.fromName('Another Day').then((video)=>{video.play('any');});

ytdl.cli()

  • This will create a CLI for YTDL.
  • You can run this by passing arguments.
    • node file.js -h ORnpm start -h
constytdl=require('@ytdl/ytdl');ytdl.cli(process.argv);

CLI (ytdl)

Examples

ytdl -d -l"https://www.youtube.com/watch?v=fJ9rUzIMcZQ" -fn"rhapsody.mp3" -ao
ytdl -p -l"https://www.youtube.com/watch?v=fJ9rUzIMcZQ" -q"360p" --set-player"mplayer"# Add -ao to play only audio from your command line.
  • Play "Another Day" from YouTube on your local media player.
ytdl -p -n"Another Day" -ao# Searches "Another Day" on YouTube and plays the first result.

Usage

Usage: ytdl [options]Options:  -V, --version                output the version number  -l, --link <url>             set the url for the YouTube video  -n, --name <name>            search by name instead of link  -i, --info                   info about YouTube link  -d, --download               download from YouTube link  -p, --play                   play YouTube media in your media player  --set-player <media-player>  set the media player  -fn, --filename <filename>   filename of downloaded content  -q, --quality <quality>      quality of downloaded content  -s, --size                   get the size of the video to be downloaded  -ao, --audio-only            download only audio stream  -vo, --video-only            download only video stream  -h, --help                   display help for command

Contributing

Contributing guidelines have been establishedhere.

License

MIT


[8]ページ先頭

©2009-2025 Movatter.jp