#15 – Descargador de canales de youtube para Peertube

…ya estamos a la versión 15 de este script!
En esta he corregido fallos en la parte donde se añade los subtitulos en español a los vídeos en inglés.

#!/bin/bash

# Substitute wrapper for peertube-import-videos.js.
# This script uses youtube-dl and jq to download the youtube videos and upload them to your PeerTube instance
# using the peertube-upload.js script from the PeerTube CLI suite.
#
# Works on Debian10 Not tested on other distributions.
#
# sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
# sudo chmod a+rx /usr/local/bin/youtube-dl
# sudo apt install jq -y
#
# I suggest you use "screen" to put the script in the background.
# Channel import can take many hours (or days) and if your session is via ssh, it could crash due network problems.
#
# Change variable fields
# IN="Channel URL or PlayList URL"
# resolution="18" # 18 = 360p (640x360 mp4) | 22 = 720p (1280x720 mp4)
# channel="PeerTube Channel"  Please create it before and paste here the sanitize nambre without spaces and lowercase
# server="https://yourPeerTubeInstanceUrl"
# username="username of PeerTube Instance"
# password="yourPassword"
#
# embedded translated subtitles!!
# If you have a YouTube channel in Japanese and you want the videos with subtitles in Spanish, put "es" in "language"
#
# Possibility of using your browser's cookies.
# Cookies must be exported using the Firefox extension "cookies.txt".
# Once you have exported the yotube cookies from your browser, copy the cookies.txt file to the server where 
# peertube works with the channel downloader. I put them in /root/cookies.txt, but you can use any other place.
#
# Possibility to use a proxy (to change your IP)
#
# please before run this script install the CLI tools following the documentation
# https://docs.joinpeertube.org/maintain-tools
# ... and then register a remote user with
# peertube auth add -u 'PEERTUBE_URL' -U 'PEERTUBE_USER' --password 'PEERTUBE_PASSWORD'
# peertube auth list.
#
# on 2022-08-02
# maxlinux2000@gmail.com
# maxlinux2000@tormentasolar.win

#############################################################

channel="" # insert the Peertube channel name, like "profesorpardalbrasil"  or "ea3grn"
IN="" # Youtube url channel


resolution="18" # youtube-dl -F https://youtube<channel> to have the listo of the other avalaible resolutions
server="https://videos.tormentasolar.win" # change it!
username="maxlinux2000"  # change it!
password="MyPass" # change it!


## force embed subtitles in language of your choice, i.e.: fr es de it ru ch
## if empty no subtitles will be embedded
language=""

# proxy
# proxy=' --proxy http://123.123.123.123:8080' 
proxy='' # no proxy


# coockies form firefox extension "cookies.txt"
#cookies=/root/cookies.txt



###############################################################################################################################
###############################################################################################################################
# From here do not touch anything unless you know what you are doing.


# install jq

if [ -f /usr/bin/jq ]; then
    echo "jq installed"
else
    clear
    echo "please install jq!

apt install jq

    "
    exit
fi

if [ -f /usr/local/bin/youtube-dl ]; then
    echo "youtube-dl installed"
else
    clear
    echo "please install youtube-dl!

sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
sudo chmod a+rx /usr/local/bin/youtube-dl

    "
    exit
fi


countdown() {
    secs=60
    while [ $secs -gt 0 ]; do
       echo -ne "$secs\033[0K\r"
       sleep 1
       : $((secs--))
    done
}


standard() {
    id=$1
    echo id=$id
    yt_code=$(echo $id | sed 's|^MINUS|-|g')
#    youtube-dl --cookies $cookies -u $yt_user -p $yt_pass $proxy --write-info-json -f "$resolution" "https://www.youtube.com/watch?v=$yt_code"

    while :
    do
        FF=$(ps -A | grep "ffmpeg" | tail -n 1 | tr -d ' ' | tr -d ':')
        if [ -z $FF ]; then
            echo "FFMPEG is not active, continuing..."
            break
        else 
            echo "FFMPEG is running: waiting 60 secs"
            countdown
        fi
    done

    youtube-dl  $cookies $proxy --write-info-json --write-thumbnail -f "$resolution" -o $PWD/$channel/$id.mp4 "https://www.youtube.com/watch?v=$yt_code" 

    json=$(cat -- $PWD/$channel/$id.info.json)
    title=$(echo $json | jq .fulltitle | tr -d '"')
    datejson=$(cat --  $PWD/$channel/$id.info.json | less | jq .upload_date | tr -d '"')
    date=$(date -d $datejson  +%Y-%m-%d)
    file=$(ls $PWD/$channel/$id.mp4)
    if [ -f $PWD/$channel/$id.jpg ]; then
        thumb=$(ls $PWD/$channel/$id.jpg)
    fi
    if [ -f $PWD/$channel/$id.webp ]; then
        thumb=$(ls $PWD/$channel/$id.webp)
    fi
    echo file=$file

    if [ ! -f "$file" ]; then
        fail=$(echo $id)
    fi
    echo fail=$fail

    description=$(echo $json | jq .description | tr -d '"' | sed 's|\\n|\n|g')
    lang=$(echo $json | jq . | grep "lang=" | head -n1 | cut -d '?' -f2 | cut -d '&' -f1 | sed 's|lang=||g')
    node $PWD/dist/server/tools/peertube-upload.js -n "$title" -C "$channel" -b $thumb -d "$date - $yt_code - $description" -L "$lang" -U "$username" -p "$password" -f "$file"  -u $server
    rm   $PWD/$channel/$file  2>/dev/null
    rm $PWD/$channel/$id.info.json	 2>/dev/null
    rm $PWD/$channel/$id.jpg 2>/dev/null

#echo function
#exit 0
}

subtitle() {
    id=$1
    echo id=$id
    yt_code=$(echo $id | sed 's|^MINUS|-|g')

    while :
    do
        FF=$(ps -A | grep "ffmpeg" | tail -n 1 | tr -d ' ' | tr -d ':')
        if [ -z $FF ]; then
            echo "FFMPEG is not active, continuing..."
            break
        else 
            echo "FFMPEG is running: waiting 60 secs"
            countdown
        fi
    done

    youtube-dl  $cookies $proxy --write-info-json --write-thumbnail -f "$resolution" -o $PWD/$channel/$id.mp4 "https://www.youtube.com/watch?v=$yt_code" 

#    youtube-dl  $cookies $proxy --write-info-json --write-thumbnail  -f "$resolution"  --write-auto-sub --sub-lang $language  -o "$PWD/$channel/$id.mp4" "https://www.youtube.com/watch?v=$yt_code"  -o $PWD/$channel/$id.$language.vtt
    youtube-dl  $cookies $proxy --write-info-json --write-thumbnail  -f "$resolution"  --write-auto-sub --sub-lang $language --skip-download  "https://www.youtube.com/watch?v=$yt_code"  -o $PWD/$channel/$id.$language.vtt

    if [ ! -f $PWD/$channel/$id.$language.vtt ]; then
        youtube-dl  $cookies --write-sub --sub-lang $language  --skip-download "https://www.youtube.com/watch?v=$yt_code" -o $PWD/$channel/$id.$language.vtt
    fi



file=$PWD/$channel/$id-$language.mp4

#9n-yQKL9nYs.es.vtt.info.json
    json=$(cat -- $PWD/$channel/$id.$language.vtt.info.json)
#    json=$(cat -- $PWD/$channel/$id.info.json)
    title=$(echo $json | jq .fulltitle | tr -d '"')
#    datejson=$(cat --  $PWD/$channel/$id.info.json | less | jq .upload_date | tr -d '"')
    datejson=$(cat --  $PWD/$channel/$id.$language.vtt.info.json | less | jq .upload_date | tr -d '"')
    date=$(date -d $datejson  +%Y-%m-%d)
    echo file=$file



    description=$(echo $json | jq .description | tr -d '"' | sed 's|\\n|\n|g')
    if [ -f $id.jpg ]; then
        thumb=$(ls $PWD/$channel/$id.jpg)
    fi
    if [ -f $PWD/$channel/$id.webp ]; then
        thumb=$(ls $PWD/$channel/$id.webp)
    fi
    lang=$(echo $json | jq . | grep "lang=" | head -n1 | cut -d '?' -f2 | cut -d '&' -f1 | sed 's|lang=||g')

echo lang=$lang

# /root/PeerTube/manabouttools/9n-yQKL9nYs.es.vtt.es.vtt

    if [ -f $PWD/$channel/$id.$language.vtt.$language.vtt ]; then
    echo "-----------------------
    Lang=$lang
    "
    ffmpeg -hide_banner -threads 1 -i "$PWD/$channel/$id.$language.vtt.$language.vtt" $PWD/$channel/sub.ass -y


    ffmpeg -hide_banner -threads 1 -i "$PWD/$channel/$id.mp4" -vf "ass=$PWD/$channel/sub.ass" -empty_hdlr_name 1 "$PWD/$channel/$id-$language.mp4" -y



        rm $PWD/$channel/sub.ass $PWD/$channel/$id.$language.vtt.$language.vtt $PWD/$channel/$id.mp4

echo "----------------------

        echo node $PWD/dist/server/tools/peertube-upload.js -n "$title" -C "$channel" -b $thumb  -d "$date - $id - $description" -L "$lang" -U "$username" -p "$password" -f "$file"  -u $server  \

---------------------
"
    if [ ! -f "$file" ]; then
        fail=$(echo $id)
    fi

    echo fail=$fail

        node $PWD/dist/server/tools/peertube-upload.js -n "$title" -C "$channel" -b $thumb -d "$date - $yt_code - $description" -L "$lang" -U "$username" -p "$password" -f "$file"  -u $server
        rm $PWD/$channel/$id.mp4  $PWD/$channel/$id-$language.mp4 $PWD/$channel/$id.info.json  $PWD/$channel/$id.jpg  $PWD/$channel/$id.webp 2>/dev/null
#        && rm ./$id.mp4  ./$id-$language.mp4 && rm ./$id.info.json && rm ./$id.jpg

    echo "----------------------"
    else
    echo "-----------------------
    Lang=en
    skipping. Please at the end, open the $channel-yt-2-translate.txt file go to youtube and generate the translation 
whatching the videos (all) with localized subtitles. Then repeat the process.
    "
        file=$PWD/$channel/$id.mp4

        node $PWD/dist/server/tools/peertube-upload.js -n "$title" -C "$channel" -b $thumb -d "$date - $yt_code - $description" -L "$lang" -U "$username" -p "$password" -f "$file"  -u "$server" 
        rm $PWD/$channel/$id.mp4 $PWD/$channel/$id-$language.mp4  $PWD/$channel/$id.info.json  $PWD/$channel/$id.jpg  $PWD/$channel/$id.webp 2>/dev/null



        echo $yt_code >> $channel-yt-2-translate.txt
        cat $channel-yt-2-translate.txt | sort | uniq > tmp2
        mv tmp2 $channel-yt-2-translate.txt

    echo "----------------------"
    fi
}


processLog() {
    fail=$1

    echo failprocess=$fail

    yt_code=$(echo $id | sed 's|^MINUS|-|g')
    cat -- $PWD/$channel/$channel-yt-list | grep -v -- "$yt_code" > $PWD/$channel/tmp1
    echo "$yt_code" >> $PWD/$channel/$channel-yt-done
    mv $PWD/$channel/tmp1 $PWD/$channel/$channel-yt-list

    if  [ -z $fail ]; then
        echo $fail >> $PWD/$channel/$channel-yt-fail
        sed -i '1s/^/$fail\n/' $PWD/$channel/$channel-yt-list
        fail=''
    fi

}

#touch ./$channel-yt-done
#:> $channel-yt-list
### old #youtube-dl  --cookies $cookies  -u $yt_user -p $yt_pass --verbose $proxy --playlist-reverse --get-id $IN | tee -a $channel-yt-list
#youtube-dl  --cookies $cookies  --verbose $proxy --playlist-reverse --get-id $IN | tee -a $channel-yt-list
#cat $channel-yt-list | uniq > tmp
#mv tmp $channel-yt-list


PWD=$(pwd)
mkdir -p  $channel
touch $PWD/$channel/$channel-yt-done
touch $channel/$channel-yt-list 


cd $channel
ls *.part | cut -d '.' -f1 > tmp
for i in `cat tmp`; do
    sed -i "s/$i//g" $channel-yt-done
    sed -i "s/$i//g" $channel-yt-list
    sed -i "1s/^/$i\n/" $channel-yt-list
done
rm tmp
cd ..



if [ ! -s $PWD/$channel/$channel-yt-list ]; then
    echo "File  $channel-yt-list IS EMPTY"
    youtube-dl  $cookies  --verbose $proxy --playlist-reverse --get-id $IN | tee -a $PWD/$channel/$channel-yt-list
    cat $PWD/$channel/$channel-yt-list | uniq > $PWD/$channel/tmp
    mv $PWD/$channel/tmp $PWD/$channel/$channel-yt-list
else 
    echo "File $channel-yt-list HAS SOMETHING"
fi


for id in $(cat -- $PWD/$channel/$channel-yt-list); do
    echo processing $id

    pre=$(cat --  $PWD/$channel/$channel-yt-done | grep -- "$id")
    echo pre=$pre
    if [ -z "$pre" ]; then pre="0" ; else echo ok; fi  2> /dev/null

        if [ "$id" == "$pre" ]; then
            echo "$id already downloaded"
        else
            id=$(echo $id | sed 's|^-|MINUS|g')
            if [ -z $language ]; then
                #mode=standard
                standard $id
                processLog $fail
                echo fail3=$fail

            else
                #mode=subtitle
                subtitle $id
                processLog $fail
                echo fail4=$fail

            fi
    fi
    echo "-------------------------"
done
cd $PWD/$channel/
rm *.mp4 *.webp *.jpg *.info.json *.vtt 2>/dev/null

Comentarios cerrados.