用于从播放列表下载youtube文件的Shell脚本

时间:2012-01-19 05:34:15

标签: bash shell download cut youtube-dl

我正在尝试编写一个bash脚本,该脚本将从播放列表中下载所有YouTube视频,并根据youtube视频本身的标题将其保存为特定的文件名。到目前为止,我有两个独立的代码片段可以完成我想要的但我不知道如何将它们组合在一起作为一个单元。

这段代码可以找到给定页面上所有YouTube视频的标题:

curl -s "$1" | grep '<span class="title video-title "' | cut -d\> -f2 | cut -d\< -f1

这段代码将文件下载到youtube视频ID给出的文件名(例如youtube.com/watch?v=CsBVaJelurE&feature=relmfu给出的文件名为CsBVaJelurE FLV的)

curl -s "$1" | grep "watch?" | cut -d\" -f4| while read video; 
do youtube-dl "http://www.youtube.com$video";
done

我想要一个脚本将youtube .flv文件输出到视频标题给出的文件名(在本例中为BASH课程2.flv),而不仅仅是视频ID名称。提前感谢所有的帮助。

5 个答案:

答案 0 :(得分:24)

好的,经过进一步的研究和更新我的youtube-dl版本后,事实证明这个功能现在直接构建到程序中,无需使用shell脚本来解决youtube上的播放列表下载问题。完整的文档可以在这里找到:(http://rg3.github.com/youtube-dl/documentation.html)但我原来问题的简单解决方案如下:

1)youtube-dl会自动处理播放列表链接,无需单独提供其中包含的视频的网址(这样就无需使用grep搜索“watch?”来查找独特的视频ID

2)现在有一个选项可以使用各种选项格式化文件名,包括:

  • id:序列将被视频标识符替换。
  • url:序列将被视频网址替换。
  • 上传者:序列将被上传视频的人的昵称取代。
  • upload_date:序列将被YYYYMMDD格式的上传日期替换。
  • title:序列将被文字视频标题取代。
  • ext:序列将被相应的扩展名替换(如 flv或mp4)。
  • epoch:创建时,序列将被Unix纪元取代 文件。
  • autonumber:序列将替换为五位数字 每次下载都会增加,从零开始。

此输出选项的语法如下(其中NAME是上面显示的任何选项):

youtube-dl -o '%(NAME)s' http://www.youtube.com/your_video_or_playlist_url

例如,要回答我原来的问题,语法如下:

youtube-dl -o '%(title)s.%(ext)s' http://www.youtube.com/playlist?list=PL2284887FAE36E6D8&feature=plcp

再次感谢那些回答我问题的人,非常感谢你的帮助。

答案 1 :(得分:1)

如果您想将youtube页面中的标题用作文件名,则可以使用-t youtube-dl选项。如果您想使用“视频列表”页面中的标题,并确定每个watch?标题只有一个<span class="title video-title"网址,那么您可以使用以下内容:

#!/bin/bash

TMPFILE=/tmp/downloader-$$

onexit() {
  rm -f $TMPFILE
}

trap onexit EXIT

curl -s "$1" -o $TMPFILE

i=0
grep '<span class="title video-title "' $TMPFILE | cut -d\> -f2 | cut -d\< -f1 | while read title; do
  titles[$i]=$title
  ((i++))
done

i=0
grep "watch?" $TMPFILE | cut -d\" -f4 | while read url; do
  urls[$i]="http://www.youtube.com$url"
  ((i++))
done

i=0; while (( i < ${#urls[@]} )); do
  youtube-dl -o "${titles[$i]}.%(ext)" "${urls[$i]}"
  ((i++))
done

我没有测试过,因为我没有“视频列表”页面示例。

答案 2 :(得分:0)

以下方法工作,并从youtube播放你的泰坦尼克号

youtube-downloader.sh youtube-video-url.sh

#!/bin/bash
decode() {
    to_decode='s:%([0-9A-Fa-f][0-9A-Fa-f]):\\x\1:g'
    printf "%b" `echo $1 | sed 's:&:\n:g' | grep "^$2" | cut -f2 -d'=' | sed -r $to_decode`
}
data=`wget http://www.youtube.com/get_video_info?video_id=$1\&hl=pt_BR -q -O-`
url_encoded_fmt_stream_map=`decode $data 'url_encoded_fmt_stream_map' | cut -f1 -d','`
signature=`decode $url_encoded_fmt_stream_map 'sig'`
url=`decode $url_encoded_fmt_stream_map 'url'`
test $2 && name=$2 || name=`decode $data 'title' | sed 's:+: :g;s:/:-:g'`
test "$name" = "-" && name=/dev/stdout || name="$name.vid"
wget "${url}&signature=${signature}" -O "$name"






#!/usr/bin/env /bin/bash
function youtube-video-url {
    local field=
    local data=
    local split="s:&:\n:g"
    local decode_str='s:%([0-9A-Fa-f][0-9A-Fa-f]):\\x\1:g'
    local yt_url="http://www.youtube.com/get_video_info?video_id=$1"
    local grabber=`command -v curl`
    local args="-sL"
    if [ ! "$grabber" ]; then
        grabber=`command -v wget`
        args="-qO-"
    fi
    if [ ! "$grabber" ]; then
        echo 'No downloader available.' >&2
        test x"${BASH_SOURCE[0]}" = x"$0" && exit 1 || return 1
    fi
    function decode {
        data="`echo $1`"
        field="$2"
        if [ ! "$field" ]; then
            field="$1"
            data="`cat /dev/stdin`"
        fi
        data=`echo $data | sed $split | grep "^$field" | cut -f2 -d'=' | sed -r $decode_str`
        printf "%b" $data
    }
    local map=`$grabber $args $yt_url | decode 'url_encoded_fmt_stream_map' | cut -f1 -d','`
    echo `decode $map 'url'`\&signature=`decode $map 'sig'`
}
[ $SHLVL != 1 ] && export -f youtube-video-url

bash youtube-player.sh saalGKY7ifU

#!/bin/bash
decode() {
    to_decode='s:%([0-9A-Fa-f][0-9A-Fa-f]):\\x\1:g'
    printf "%b" `echo $1 | sed 's:&:\n:g' | grep "^$2" | cut -f2 -d'=' | sed -r $to_decode`
}


data=`wget http://www.youtube.com/get_video_info?video_id=$1\&hl=pt_BR -q -O-`


url_encoded_fmt_stream_map=` decode $data 'url_encoded_fmt_stream_map' | cut -f1 -d','`

signature=` decode $url_encoded_fmt_stream_map 'sig'`

url=`decode $url_encoded_fmt_stream_map 'url'`
test $2 && name=$2 || name=`decode $data 'title' | sed 's:+: :g;s:/:-:g'`


test "$name" = "-" && name=/dev/stdout || name="$name.mp4"

# // wget "${url}&signature=${signature}" -O "$name"

mplayer -zoom -fs "${url}&signature=${signature}"

它使用您可能已安装的解码和bash。

答案 3 :(得分:0)

我使用此bash脚本从给定的youtube播放列表中下载一组给定的歌曲

#!/bin/bash
downloadDirectory = <directory where you want your videos to be saved>
playlistURL = <URL of the playlist>
for i in {<keyword 1>,<keyword 2>,...,<keyword n>}; do
    youtube-dl -o ${downloadDirectory}"/youtube-dl/%(title)s.%(ext)s" ${playlistURL} --match-title $i
done

注意:“关键字i”是该播放列表中给定视频的标题(整体或部分;如果是该部分,则该播放列表应该是唯一的)。

编辑:您可以通过 pip install youtube-dl

安装youtube-dl

答案 4 :(得分:-2)

#!/bin/bash
# Coded by Biki Teron 
# String replace command in linux

echo "Enter youtube url:"
read url1

wget -c -O index.html $url1
################################### Linux string replace    ##################################################
sed -e 's/%3A%2F%2F/:\/\//g'  index.html > youtube.txt
sed -i 's/%2F/\//g' youtube.txt
sed -i 's/%3F/?/g' youtube.txt
sed -i 's/%3D/=/g' youtube.txt
sed -i 's/%26/\&/g'  youtube.txt
sed -i 's/%252/%2/g'  youtube.txt
sed -i 's/sig/&signature/g'  youtube.txt
## command to get filename
nawk '/<title>/,/<\/title>/' youtube.txt > filename.txt ## Print the line between   containing <title> and <\/title> .
sed -i 's/.*content="//g' filename.txt 
sed -i 's/">.*//g' filename.txt
sed -i 's/.*<title>//g' filename.txt
sed -i 's/<.*//g' filename.txt
######################################## Coding to get all itag list ########################################

nawk '/"fmt_list":/,//' youtube.txt > fmt.html ## Print the line containing "fmt_list": .
sed -i 's/.*"fmt_list"://g' fmt.html
sed -i 's/, "platform":.*//g' fmt.html
sed -i 's/, "title":.*//g' fmt.html

# String replace command in linux to get correct itag format
sed -i 's/\\\/1920x1080\\\/99\\\/0\\\/0//g'  fmt.html  ## Replace \/1920x1080\/99\/0\/0 by blank .
sed -i 's/\\\/1920x1080\\\/9\\\/0\\\/115//g' fmt.html  ## Replace \/1920x1080\/9\/0\/115 by blank.
sed -i 's/\\\/1280x720\\\/99\\\/0\\\/0//g'   fmt.html  ## Replace \/1280x720\/99\/0\/0 by blank.
sed -i 's/\\\/1280x720\\\/9\\\/0\\\/115//g'  fmt.html  ## Replace \/1280x720\/9\/0\/115 by blank.
sed -i 's/\\\/854x480\\\/99\\\/0\\\/0//g'    fmt.html  ## Replace \/854x480\/99\/0\/0 by blank.
sed -i 's/\\\/854x480\\\/9\\\/0\\\/115//g'   fmt.html  ## Replace \/854x480\/9\/0\/115 by blank.
sed -i 's/\\\/640x360\\\/99\\\/0\\\/0//g'    fmt.html  ## Replace \/640x360\/99\/0\/0 by blank.
sed -i 's/\\\/640x360\\\/9\\\/0\\\/115//g'   fmt.html  ## Replace \/640x360\/9\/0\/115 by blank.
sed -i 's/\\\/640x360\\\/9\\\/0\\\/115//g'   fmt.html  ## Replace \/640x360\/9\/0\/115 by blank.
sed -i 's/\\\/320x240\\\/7\\\/0\\\/0//g'     fmt.html  ## Replace \/320x240\/7\/0\/0 by blank.
sed -i 's/\\\/320x240\\\/99\\\/0\\\/0//g'    fmt.html  ## Replace \/320x240\/99\/0\/0 by blank.
sed -i 's/\\\/176x144\\\/99\\\/0\\\/0//g'    fmt.html  ## Replace \/176x144\/99\/0\/0 by blank.

# Command to cut a part of a file between any two strings
nawk '/"url_encoded_fmt_stream_map":/,//' youtube.txt > url.txt
sed -i 's/.*url_encoded_fmt_stream_map"://g' url.txt

#Display video resolution information
echo ""
echo "Video resolution:"
echo "[46=1080(.webm)]--[37=1080(.mp4)]--[35=480(.flv)]--[36=180(.3gpp)]"
echo "[45=720 (.webm)]--[22=720 (.mp4)]--[34=360(.flv)]--[17=144(.3gpp)]"
echo "[44=480 (.webm)]--[18=360 (.mp4)]--[5=240 (.flv)]"
echo "[43=360 (.webm)]"
echo ""
echo "itag list= "`cat fmt.html`
echo "Enter itag number: "
read fmt

####################################### Coding to get required resolution #################################################
## cut itag=?

sed -e "s/.*,itag=$fmt//g" url.txt > "$fmt"_1.txt
sed -e 's/\u0026quality.*//g' "$fmt"_1.txt > "$fmt".txt
sed -i 's/.*u0026url=//g' "$fmt".txt ## Ignore all lines before \u0026url= but print all lines after \u0026url=.
sed -e 's/\u0026type.*//g' "$fmt".txt > "$fmt"url.txt ## Ignore all lines after \u0026type but print all lines before \u0026type.
sed -i 's/\\/\&/g' "$fmt"url.txt ## replace \ by &
sed -e 's/.*\u0026sig//g' "$fmt".txt > "$fmt"sig.txt ## Ignore all lines before \u0026sig but print all lines after \u0026sig.
sed -i 's/\\/\&ptk=machinima/g' "$fmt"sig.txt ## replace \ by &
echo `cat "$fmt"url.txt``cat "$fmt"sig.txt` > "$fmt"url.txt ## Add string at the end of a line
echo `cat "$fmt"url.txt` > link.txt ## url and signature content to 44url.txt
rm "$fmt"sig.txt
rm "$fmt"_1.txt
rm "$fmt".txt
rm "$fmt"url.txt
rm youtube.txt
########################################### Coding for filename with correct extension #####################################
if [ $fmt -eq 46 ]
 then 
    echo `cat filename.txt`.webm > filename.txt

elif [ $fmt -eq 45 ]
  then 
     echo `cat filename.txt`.webm > filename.txt

elif [ $fmt -eq 44 ]
  then 
     echo `cat filename.txt`.webm > filename.txt

elif [ $fmt -eq 43 ]
   then 
   echo `cat filename.txt`.webm > filename.txt

elif [ $fmt -eq 37 ]
   then 
   echo `cat filename.txt`.mp4 > filename.txt

elif [ $fmt -eq 22 ]
   then 
   echo `cat filename.txt`.mp4 > filename.txt

elif [ $fmt -eq 18 ]
   then 
  echo `cat filename.txt`.mp4 > filename.txt

elif [ $fmt -eq 35 ]
   then 
   echo `cat filename.txt`.flv > filename.txt

elif [ $fmt -eq 34 ]
   then 
   echo `cat filename.txt`.flv > filename.txt

elif [ $fmt -eq 5 ]
   then 
   echo `cat filename.txt`.flv > filename.txt

elif [ $fmt -eq 36 ]
   then 
   echo `cat filename.txt`.3gpp > filename.txt

 else
   echo `cat filename.txt`.3gpp > filename.txt

fi
rm fmt.html
rm url.txt
filename=`cat filename.txt`
linkdownload=`cat link.txt`
wget -c -O "$filename" $linkdownload
echo "Download Finished!"
read