Nueva actualización de youtube->peertube

Después que youtube me haya bloqueado la IP de donde descargo los vídeos por haberme pasado 3 pueblos (si, me pasé 🙂 ) he añadido la posibilidad de utilizar un proxy, y por esto he instalado en uno de los vps que tengo en digitalocean el paquete tinyproxy. Que es un simple proxy sin mucha complicación que pero va muy bien para uso personal.
En este ejemplo asumo que el server con peertube tiene la linea de internet con la IP 100.100.100.100, mientras que el server externo donde vamos a instalar tinyproxy tiene la IP 123.123.123.123
Otra pequeña modificación que he añadido es que ahora en la descripción añade la fecha de upload del vídeo en youtube y el código de referencia, que puede ser útil también para el debug.

Aquí el script:

#!/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"
#
# 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 2021-04-10
# maxlinux2000@gmail.com


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

IN="https://www.youtube.com/channel/UCjvBN1r7UXXqmIbx_u7bIAw"
#IN="https://www.youtube.com/watch?v=QwD4_sxGEQU" # TEST

resolution="18"
channel="opensourceecology"
server="https://videos.tormentasolar.win"
username=maxlinux2000
password="MyPassword"

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

# proxy
# if empty it will not used
# proxy="--proxy https://123.456.678.1:8080"
proxy="--proxy https://123.123.123.123:6789"

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

PWD=$(pwd)

standard() {
    id=$1
    echo id=$id
    youtube-dl $proxy --write-info-json -f "$resolution" $id
    json=$(cat $id.info.json)
    title=$(echo $json | jq .fulltitle | tr -d '"')
    file=$(ls $PWD/$id.mp4)
    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" -d "$description" -L "$lang" -U "$username" -p "$password" -f "$file" && \
    rm "$file" &&  rm $id.info.json
}

subtitle() {
    id=$1
    echo id=$id
    youtube-dl $proxy --write-info-json -f "$resolution"  --write-auto-sub --sub-lang $language  -o $id.mp4 $id

    if [ ! -f $id.$language.vtt ]; then
        youtube-dl  $proxy  --write-info-json -f "$resolution" --write-sub --sub-lang $language -o $id.mp4 $id
    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')
    lang=$(echo $json | jq . | grep "lang=" | head -n1 | cut -d '?' -f2 | cut -d '&' -f1 | sed 's|lang=||g')

    if [ -f $id.$language.vtt ]; then
        ffmpeg -hide_banner -loglevel error  -i $id.$language.vtt sub.ass -y
        ffmpeg -hide_banner -loglevel error  -i $id.mp4 -vf "ass=sub.ass" $id-$language.mp4 -y
        rm sub.ass $id.$language.vtt
        node $PWD/dist/server/tools/peertube-upload.js -n "$title" -C "$channel" -d "$date - $id - $description" -L "$lang" -U "$username" -p "$password" -f "$file"   && rm  $id.mp4  $id-$language.mp4 && rm $id.info.json
    else
        file=$PWD/$id.mp4
        node $PWD/dist/server/tools/peertube-upload.js -n "$title" -C "$channel" -d "$date - $id - $description" -L "$lang" -U "$username" -p "$password" -f "$file"   && rm  $id.mp4  && rm $id.info.json
    fi
}

processLog() {
    cat $channel-yt-list | grep -v -- "$id" > tmp1
    mv tmp1 $channel-yt-list
    echo ok2
    echo "$id" >> $channel-yt-done
}

touch $channel-yt-done

:> $channel-yt-list
youtube-dl $proxy --playlist-reverse --get-id $IN | tee -a $channel-yt-list

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
            if [ -z $language ]; then
                #mode=standard
                standard $id && processLog
            else
                #mode=subtitle
                subtitle $id && processLog
            fi
    fi
    echo "-------------------------"
done

Se puede usar cualquier proxy https, pero si quieres usar tinyproxy abre un terminal ssh de tu server remoto que supongamos tenga la ip 123.123.123.123 (tiene que tener linea de internet diferente de la donde está peertube ). Instala este paquete:

apt install tinyproxy

luego modifica la configuración

mcedit /etc/tinyproxy.conf

y modifica estas dos variables

Port 6789  
Allow 100.100.100.100 # la ip de la linea donde está peertube

La puerta por defecto es la 8888, pero tu puedes usar cualguiera otra por arriba de la 1024.
De esta forma el proxy va a funcionar solo para la IP 100.100.100.100 y no va a servir todos los demás que se conecte y descubra el proxy.
Reinicia el servicio con

service tinyproxy restart

…y ya puedes descargar la otra mitad de youtube 🙂

Comentarios cerrados.