R/videoListDownloadByTable.R

Defines functions videoListDownloadByTable

Documented in videoListDownloadByTable

#' Download YouTube videos/audios by providing a table of video/audio list.
#' @param fileList the file name of video/audio list table. This table
#' is a csv file generated by \code{\link{videoListTable}} function.
#' The columns of this table are 'rowNumber', 'Orders',
#' 'VideoTitles', 'fileName', and 'URL', in which 'URL' is most
#' important information. If fileTable is provided, then this argument is
#' not used.
#' @param fileTable the file table returned by \code{\link{videoListTable}}
#' function. If fileTable is provided, then fileList argument is
#' not used.
#' @param path the path to save video/audio files.
#' @param bothVideoAudio logic, whether download both audio and video,
#' it should be FALSE if only downloading audio or video. The default
#' is \code{TRUE}.
#' @param priority the file format to download. The option can be
#' any one (or combination) of "mp4", "best", "audio only", "mp3",
#' "webm", the default
#' is c("mp4", "best", "audio only"), which means the downloader will
#' look for mp4 file first, if this file does not exist, then the
#' downloader will look for the best file marked by YouTube, then
#' look for only audio file.
#' @param id numeric or \code{NULL}, the index of rows of the `fileList`
#' table to download. The default is \code{NULL}.
#' @param sleepTime numeric, the time to pause to prevent YouTube blocking.
#' The default is 10 second
#' @import curl
#' @importFrom limma strsplit2
#' @import stringr
#' @export
#' @examples {
#' \dontrun{
#' file = "fileNameOrders.csv"
#' folderName = "./fileByTable"
#' #### download video by fileNameOrders.csv table
#' newFolder = videoListDownloadByTable(fileList = file,
#'                                      folderName = folderName,
#'                                      bothVideoAudio = TRUE,
#'                                      priority = c("best"),
#'                                      sleepTime = 10,
#'                                      id = c(1, 2, 5, 9))
#' }
#' }

videoListDownloadByTable = function(fileList,
                                    fileTable = NULL,
                                    path,
                                    bothVideoAudio = TRUE,
                                    priority = c("mp4", "best", "audio only"),
                                    id = NULL,
                                    sleepTime = 10) {
  # download video by table
  print(path)
  if (is.null(fileTable)){
    fileName = fileList
    if (!dir.exists(path))
      dir.create(path, recursive = TRUE)
    # save file name and URLs
    orderTitle = read.csv(fileName, header = TRUE, row.names = 1)
  } else {
    orderTitle = fileTable
  }
  if (is.null(id)) {
    id = 1:nrow(orderTitle)
  }

  id = id[id %in% (1:nrow(orderTitle))]
  # download videos
  for (ui in id) {
    if(orderTitle[ui, 2] != "Private_Video"){
      url_ui = orderTitle[ui, 4]
      print(url_ui)
      file_ui = orderTitle[ui, 3]
      youTubeDownload(
        url = url_ui,
        path = path,
        saveFile = file_ui,
        priority = priority,
        bothVideoAudio = bothVideoAudio
      )
      Sys.sleep(abs(rnorm(1, sleepTime, 5)))
    } else {
      cat("Private video:\n", id, " :", orderTitle[ui, 3])
    }
  }
  print("All Done!")
  return(path)
}
paodan/youtubeDownloader documentation built on Nov. 15, 2020, 9:48 p.m.