【问题标题】:Downloading the first video from each search result from Youtube with youtube-dl?使用 youtube-dl 从 Youtube 的每个搜索结果中下载第一个视频?
【发布时间】:2016-03-24 03:50:54
【问题描述】:

我有一个包含 400 首歌曲名称的列表,并将它们超链接到搜索结果页面。 Example picture

我有 youtube-dl 和 J Downloader,但不知道我需要 youtube-dl 中的哪些参数才能从视频的搜索 URL 列表中下载高质量的 mp3?我只希望它将每次搜索的第一个视频下载为 mp3。

【问题讨论】:

    标签: video cmd youtube youtube-dl jdownloader


    【解决方案1】:

    我写了一个 Ruby 脚本(youtube-dl 上的包装器),我用它来下载 音频 - 你可以看到它here

    提取音频的代码是:

    DESTINATION_PATH="/home/max/Downloads"
    URL="https://www.youtube.com/watch?v=cASW7BFWf6U"
    cd $DESTINATION_PATH && youtube-dl --extract-audio --prefer-ffmpeg --audio-format mp3 --yes-playlist --audio-quality 3 $URL`
    

    有了这个,您可以使用您选择的 HTML 解析库来获取第一个视频 关闭 youtube 的搜索结果。我个人有过Nokogiri的经验, 来自here 看来您可以使用命令行工具。

    例如,

    CSS_SELECTOR="#selector_of_the_first_video"
    curl -s $URL | nokogiri -e 'puts $_.at_css("$CSS_SELECTOR").text'
    

    【讨论】:

      【解决方案2】:

      您的问题没有解释您要对列表的其余部分做什么。无论如何,我将向您展示如何获取第一个链接的 MP3。

      1. 首先,用逗号 (,) 分隔您的网址
      2. 现在用 PHP 获取整个文件

        $file = 'path_to_file';
        $data = file_get_contents($file);
        
      3. 把列表变成数组

        $songs_list = explode(",", $data);
        
      4. 设置计数并循环遍历数组

        foreach ($songs_list as $key => $song) {
            if ($count == 1) {
                $commad = 'youtube-dl --extract-audio --audio-format mp3 youtube_video_url_here';
                shell_exec($commad); // now audio of first video will be downloaded as MP3
            } else {
               // do the rest of your work on list
            }
        }
        

        下面是完整的脚本

        <?php
            $file = 'path_to_file';
            $data = file_get_contents($file);
            $songs_list = explode(",", $data);
            $count = 1;
            foreach ($songs_list as $key => $song) {
                 if ($count == 1) {
                     $commad = 'youtube-dl --extract-audio --audio-format mp3 youtube_video_url_here';
                     shell_exec($commad); // now audio of first video will be downloaded as MP3
                 } else {
                     // do the rest of your work on list
                 }
           }
        ?>
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-12
        • 2021-06-26
        • 2023-01-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多