function downloadVideo(video) {
  var id = getVideoId(video.url).id.toUpperCase();
  video.filename = `${id}-${moment().valueOf()}`;

  if (!fs.existsSync(outputPath)) {
    fs.mkdirSync(outputPath);
  }
  return new Promise((resolve, reject) => {
    ytdl.getInfo(video.url, (err, info) => {
      // Selects the hard-coded 720p ytdl format code
      var format = ytdl.chooseFormat(info.formats, { quality: "136" });

      // Checks that the video is less than one hour after the startTime
      if (format && info.length_seconds <= 3600 - (video.startTime || 0)) {
        debug("Video acceptable");
        ytdl
          .downloadFromInfo(info, { begin: `${video.startTime || 0}s` })
          .pipe(fs.createWriteStream(video.absoluteInput))
          .on("finish", () => {
            resolve(video);
          });
      } else {
        debug("Unacceptable video");
        return reject({ err: "Video too long" });
      }
    });
  });
}
/**
 * downloadVideo - Downloads the YouTube video using ytdl. Also checks that the video
 * is not too long by calculating whether more than one hour of content will have to be
 * downloaded. The startTime is factored into this. All videos are downloaded at 720p max.
 *
 * @param {Video} video The newly constructed video object
 * @param {string} video.url YouTube url
 * @return {Video} The same video object
 */
Downloads the YouTube video using ytdl. Also checks that the video
is not too long by calculating whether more than one hour of content will have to be
downloaded. The startTime is factored into this. All videos are downloaded at 720p max.

@param {Video} video The newly constructed video object
@param {string} video.url YouTube url
@return {Video} The same video object
Downloads the YouTube video using ytdl . Also checks that the video is not too long by calculating whether more than one hour of content will have to be downloaded . The startTime is factored into this . All videos are downloaded at 720p max . @param { Video } video The newly constructed video object @param { string } video . url YouTube url @return { Video } The same video object
function downloadVideo(video) {
  var id = getVideoId(video.url).id.toUpperCase();
  video.filename = `${id}-${moment().valueOf()}`;
  if (!fs.existsSync(outputPath)) {
    fs.mkdirSync(outputPath);
  }
  return new Promise((resolve, reject) => {
    ytdl.getInfo(video.url, (err, info) => {
      var format = ytdl.chooseFormat(info.formats, { quality: "136" });
      if (format && info.length_seconds <= 3600 - (video.startTime || 0)) {
        debug("Video acceptable");
        ytdl
          .downloadFromInfo(info, { begin: `${video.startTime || 0}s` })
          .pipe(fs.createWriteStream(video.absoluteInput))
          .on("finish", () => {
            resolve(video);
          });
      } else {
        debug("Unacceptable video");
        return reject({ err: "Video too long" });
      }
    });
  });
}
function downloadVideo ( video ) { var id = getVideoId ( video . url ) . id . toUpperCase ( ) ; video . filename = ` ${ id } ${ moment ( ) . valueOf ( ) } ` ; if ( ! fs . existsSync ( outputPath ) ) { fs . mkdirSync ( outputPath ) ; } return new Promise ( ( resolve , reject ) => { ytdl . getInfo ( video . url , ( err , info ) => { var format = ytdl . chooseFormat ( info . formats , { quality : "136" } ) ; if ( format && info . length_seconds <= 3600 - ( video . startTime || 0 ) ) { debug ( "Video acceptable" ) ; ytdl . downloadFromInfo ( info , { begin : ` ${ video . startTime || 0 } ` } ) . pipe ( fs . createWriteStream ( video . absoluteInput ) ) . on ( "finish" , ( ) => { resolve ( video ) ; } ) ; } else { debug ( "Unacceptable video" ) ; return reject ( { err : "Video too long" } ) ; } } ) ; } ) ; }
downloadVideo - Downloads the YouTube video using ytdl.
downloadVideo - Downloads the YouTube video using ytdl .
// Selects the hard-coded 720p ytdl format code // Checks that the video is less than one hour after the startTime