Nueva versión de Youtube->Peertube

Otra versión mejorada del “Descargador de Canales de youtube” con muchas mejoras y correciones de fallos


#!/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-04-2022
# maxlinux2000@gmail.com
# maxlinux2000@tormentasolar.win

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

IN="" # Youtube url channel
resolution="18" # youtube-dl -F https://youtube<channel> to have the listo of the other avalaible resolutions
channel="" # insert the Peertube channel name, like "profesorpardalbrasil"  or "ea3grn"
server="https://videos2.tormentasolar.win" # change it!
username="username"  # change it!
password="your-password" # 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 https://138.68.75.90:8888"
#proxy="--proxy https://209.97.191.124:8888"
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
}



PWD=$(pwd)

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 $cookies $proxy --write-info-json --write-thumbnail -f "$resolution" -o $id.mp4 "https://www.youtube.com/watch?v=$yt_code" 

    json=$(cat -- $id.info.json)
    title=$(echo $json | jq .fulltitle | tr -d '"')
    datejson=$(cat --  $id.info.json | less | jq .upload_date | tr -d '"')
    date=$(date -d $datejson  +%Y-%m-%d)
    file=$(ls $PWD/$id.mp4)
    if [ -f $PWD/$id.jpg ]; then
        thumb=$(ls $PWD/$id.jpg)
    fi
    if [ -f $PWD/$id.webp ]; then
        thumb=$(ls $PWD/$id.webp)
    fi
    echo file=$file
    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   "$file" &&  rm ./$id.info.json && rm ./$id.jpg 2>/dev/null
}

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 $cookies $proxy --write-info-json --write-thumbnail  -f "$resolution"  --write-auto-sub --sub-lang $language  -o "$id.mp4" "https://www.youtube.com/watch?v=$yt_code" 

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

    file=$PWD/$id-$language.mp4
    json=$(cat -- $id.info.json)
    title=$(echo $json | jq .fulltitle | tr -d '"')
    datejson=$(cat --  $id.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 $PWD/$id.jpg ]; then
        thumb=$(ls $PWD/$id.jpg)
    fi
    if [ -f $PWD/$id.webp ]; then
        thumb=$(ls $PWD/$id.webp)
    fi
    lang=$(echo $json | jq . | grep "lang=" | head -n1 | cut -d '?' -f2 | cut -d '&' -f1 | sed 's|lang=||g')


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

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  \

---------------------
"

        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 ./$id.mp4  ./$id-$language.mp4 ./$id.info.json  ./$id.jpg  ./$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/$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 ./$id.mp4 ./$id-$language.mp4  ./$id.info.json  ./$id.jpg  ./$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() {
    yt_code=$(echo $id | sed 's|^MINUS|-|g')
    cat -- $channel-yt-list | grep -v -- "$yt_code" > tmp1
    echo "$yt_code" >> ./$channel-yt-done
    mv tmp1 ./$channel-yt-list
}

#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

touch ./$channel-yt-done
if [ ! -s $channel-yt-list ]; then
    echo "File  $channel-yt-list IS EMPTY"
    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
else 
    echo "File $channel-yt-list HAS SOMETHING"
fi


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

    pre=$(cat --  ./$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
            else
                #mode=subtitle
                subtitle $id && processLog
            fi
    fi
    echo "-------------------------"
done

rm *.mp4 *.webp *.jpg *.info.json *.vtt 2>/dev/null

Comments are closed.