2025-05-30 15:52:49 +00:00
#!/bin/bash
2025-05-30 16:09:56 +00:00
#Parameters:
#1st parameter: Channel you want to turn into a playlist. Leave blank to save your subscriptions (cookie file required)
2025-05-30 15:52:49 +00:00
channel = ${ 1 :- "subscriptions" }
2025-05-30 16:09:56 +00:00
#2nd parameter: Time limit for the download. Leave blank to save all videos from the last month.
2025-05-30 15:52:49 +00:00
breaktime = ${ 2 :- "today-1month" }
2025-05-30 16:09:56 +00:00
#3rd parameter: Seconds between data requests. Decrease to make downloads faster, but your account may be temporarily blocked if you use a number too low.
2025-05-30 15:52:49 +00:00
sleeptime = ${ 3 :- "1.0" }
2025-06-02 14:21:59 +00:00
#4th parameter: Whether to enable exporting to FreeTube playlist database (1=on by default, 0=off)
enabledb = ${ 4 :- "1" }
#5th parameter: Whether to enable exporting to a CSV file (1=on by default, 0=off)
enablecsv = ${ 5 :- "1" }
2025-05-30 16:09:56 +00:00
#Internal variables:
2025-05-30 15:52:49 +00:00
#Via https://stackoverflow.com/questions/59895/how-do-i-get-the-directory-where-a-bash-script-is-located-from-within-the-script
2025-05-30 16:09:56 +00:00
folder = $( cd -- " $( dirname -- " ${ BASH_SOURCE [0] } " ) " & >/dev/null && pwd )
2025-05-30 15:52:49 +00:00
#Required to download your own subscriptions.
#Obtain this file through the procedure listed at
# https://github.com/yt-dlp/yt-dlp/wiki/FAQ#how-do-i-pass-cookies-to-yt-dlp
#and place it next to your script.
2025-05-30 16:09:56 +00:00
cookies = " ${ folder } /yt-cookies.txt "
2025-06-08 22:08:27 +00:00
subfolder = " ${ folder } /subscriptions "
2025-06-13 20:16:04 +00:00
temporary = " /tmp/subscriptions- ${ channel } "
2025-06-16 14:42:44 +00:00
if [ [ ! -w "/tmp" ] ] ; then
temporary = " ${ subfolder } /subscriptions- ${ channel } "
fi
2025-05-30 16:09:56 +00:00
archive = " ${ subfolder } / ${ channel } .txt "
2025-06-13 20:16:04 +00:00
sortcsv = " ${ temporary } / ${ channel } -sort.csv "
2025-05-30 16:09:56 +00:00
csv = " ${ subfolder } / ${ channel } .csv "
2025-06-24 15:25:48 -06:00
tmpcsv = " ${ temporary } / ${ channel } .csv "
2025-05-31 19:37:00 +00:00
json = " ${ subfolder } / ${ channel } .db "
2025-06-24 13:44:20 -06:00
python = "python3"
2025-05-30 15:52:49 +00:00
if [ [ -f "/opt/venv/bin/python" ] ] ; then
2025-05-30 16:09:56 +00:00
python = "/opt/venv/bin/python"
2025-05-30 15:52:49 +00:00
fi
2025-06-16 14:42:44 +00:00
ytdl = "yt-dlp"
if [ [ -f "/usr/bin/yt-dlp" ] ] ; then
ytdl = "/usr/bin/yt-dlp"
fi
2025-05-30 15:52:49 +00:00
if [ [ -f "/opt/venv/bin/yt-dlp" ] ] ; then
2025-05-30 16:09:56 +00:00
ytdl = "/opt/venv/bin/yt-dlp"
2025-05-30 15:52:49 +00:00
fi
2025-06-16 14:42:44 +00:00
if [ [ -f "/data/data/com.termux/files/usr/bin/yt-dlp" ] ] ; then
ytdl = "/data/data/com.termux/files/usr/bin/yt-dlp"
fi
2025-05-31 19:37:00 +00:00
if [ [ ! -d " ${ subfolder } " ] ] ; then
mkdir -v " ${ subfolder } "
2025-05-30 15:52:49 +00:00
fi
2025-06-11 00:11:24 +00:00
if [ [ ! -d " ${ temporary } " ] ] ; then
mkdir -v " ${ temporary } "
fi
cd " ${ temporary } " || exit
2025-06-02 14:21:59 +00:00
if [ [ ! -f " ${ archive } " ] ] ; then
touch " ${ archive } "
fi
2025-06-11 00:11:24 +00:00
if [ [ -f " ${ subfolder } / ${ channel } .tar.zst " ] ] ; then
2025-06-08 22:08:27 +00:00
if [ [ " ${ channel } " = "subscriptions" ] ] ; then
2025-06-11 00:11:24 +00:00
find " ${ subfolder } " -iname "*.tar.zst" | while read -r c; do tar -xvp -I zstd -f " ${ c } " ; done
2025-06-08 22:08:27 +00:00
else
2025-06-11 00:11:24 +00:00
tar -xvp -I zstd -f " ${ subfolder } / ${ channel } .tar.zst "
2025-06-08 22:08:27 +00:00
fi
2025-05-31 19:37:00 +00:00
fi
2025-05-30 16:09:56 +00:00
url = " https://www.youtube.com/@ ${ channel } "
if [ [ " ${ channel } " = "subscriptions" ] ] ; then
url = "https://www.youtube.com/feed/subscriptions"
2025-05-30 15:52:49 +00:00
fi
2025-06-12 14:02:10 +00:00
for full_url in " ${ url } /videos " " ${ url } /shorts " " ${ url } /streams " ; do
echo " ${ full_url } "
2025-06-13 20:16:04 +00:00
if [ [ -f " ${ cookies } " || " ${ channel } " = "subscriptions" ] ] ; then
2025-06-12 14:02:10 +00:00
#If available, you can use the cookies from your browser directly. Substitute
# --cookies "${cookies}"
#for the below, substituting for your browser of choice:
# --cookies-from-browser "firefox"
#In case this still fails, you can resort to a PO Token. Follow the instructions at
# https://github.com/yt-dlp/yt-dlp/wiki/PO-Token-Guide
#and add a new variable with the contents of the PO Token in the form
# potoken="INSERTYOURPOTOKENHERE"
#then substitute the "--extractor-args" line below with
# --extractor-args "youtubetab:approximate_date,youtube:player-client=default,mweb;po_token=mweb.gvs+${potoken}" \
#including the backslash so the multiline command keeps working.
" ${ python } " " ${ ytdl } " " ${ full_url } " \
--cookies " ${ cookies } " \
--extractor-args "youtubetab:approximate_date" \
--skip-download --download-archive " ${ archive } " \
--dateafter " ${ breaktime } " \
--break-on-reject --lazy-playlist --write-info-json \
2025-06-16 14:42:44 +00:00
--sleep-requests " ${ sleeptime } " \
--parse-metadata "video::(?P<formats>)" \
--parse-metadata "video::(?P<thumbnails>)" \
2025-06-16 17:21:36 +00:00
--parse-metadata "video::(?P<subtitles>)" \
2025-06-16 14:42:44 +00:00
--parse-metadata "video::(?P<automatic_captions>)" \
2025-06-16 17:43:08 +00:00
--parse-metadata "video::(?P<chapters>)" \
--parse-metadata "video::(?P<heatmap>)" \
2025-06-16 14:42:44 +00:00
--parse-metadata "video::(?P<tags>)" \
--parse-metadata "video::(?P<categories>)"
2025-06-12 14:02:10 +00:00
else
" ${ python } " " ${ ytdl } " " ${ full_url } " \
--extractor-args "youtubetab:approximate_date" \
--skip-download --download-archive " ${ archive } " \
--dateafter " ${ breaktime } " \
--break-on-reject --lazy-playlist --write-info-json \
2025-06-16 14:42:44 +00:00
--sleep-requests " ${ sleeptime } " \
--parse-metadata "video::(?P<formats>)" \
--parse-metadata "video::(?P<thumbnails>)" \
2025-06-16 17:21:36 +00:00
--parse-metadata "video::(?P<subtitles>)" \
2025-06-16 14:42:44 +00:00
--parse-metadata "video::(?P<automatic_captions>)" \
2025-06-16 17:43:08 +00:00
--parse-metadata "video::(?P<chapters>)" \
--parse-metadata "video::(?P<heatmap>)" \
2025-06-16 14:42:44 +00:00
--parse-metadata "video::(?P<tags>)" \
--parse-metadata "video::(?P<categories>)"
2025-06-12 14:02:10 +00:00
fi
done
2025-06-11 14:17:03 +00:00
if [ [ ${ enablecsv } = 1 ] ] ; then
2025-06-24 15:25:48 -06:00
if [ [ -f " ${ tmpcsv } " ] ] ; then
rm -rf " ${ tmpcsv } "
2025-06-11 14:17:03 +00:00
fi
2025-06-24 15:25:48 -06:00
touch " ${ tmpcsv } "
2025-06-09 05:07:30 +00:00
fi
2025-06-11 14:17:03 +00:00
if [ [ ${ enabledb } = 1 ] ] ; then
if [ [ -f " ${ sortcsv } " ] ] ; then
rm -rf " ${ sortcsv } "
fi
touch " ${ sortcsv } "
2025-06-02 14:21:59 +00:00
fi
2025-06-10 14:21:26 +00:00
breaktime_timestamp = $( date -d" ${ breaktime } " +"%s" )
2025-06-11 00:11:24 +00:00
count = 0
total = $( find " ${ temporary } " -type f -iname "*.info.json" | wc -l)
find " ${ temporary } " -type f -iname "*.info.json" | while read -r x; do
count = $(( count + 1 ))
2025-06-09 15:07:05 +00:00
(
2025-06-11 00:11:24 +00:00
if [ [ -f " ${ x } " && " ${ channel } " != "subscriptions" && $( jq -rc ".uploader_id" " ${ x } " ) != " @ ${ channel } " ] ] ; then
2025-06-16 17:21:36 +00:00
echo " ${ count } / ${ total } ${ x } not uploaded from ${ channel } , removing... " && rm " ${ x } "
2025-06-09 05:07:30 +00:00
fi
2025-06-11 00:11:24 +00:00
if [ [ -f " ${ x } " && " ${ breaktime } " = ~ ^[ 0-9] +$ ] ] ; then
file_timestamp = $( jq -rc '.timestamp' " ${ x } " )
2025-06-10 14:21:26 +00:00
if [ [ " ${ breaktime_timestamp } " -ge " ${ file_timestamp } " ] ] ; then
2025-06-16 17:21:36 +00:00
echo " ${ count } / ${ total } ${ x } uploaded before ${ breaktime } , removing... " && rm " ${ x } "
2025-06-10 14:21:26 +00:00
fi
2025-06-09 05:07:30 +00:00
fi
2025-06-11 00:11:24 +00:00
if [ [ -f " ${ x } " ] ] ; then
2025-06-16 17:21:36 +00:00
if [ [ $( stat -c%s " ${ x } " ) -gt 4096 ] ] ; then
2025-06-16 17:43:08 +00:00
jq '.formats="" | .automatic_captions="" | .subtitles="" | .thumbnails="" | .tags="" | .chapters="" | .heatmap="" | .categories=""' " ${ x } " >" ${ x } .tmp " && mv " ${ x } .tmp " " ${ x } "
2025-06-16 17:21:36 +00:00
fi
2025-06-13 20:16:04 +00:00
echo " youtube $( jq -cr '.id' " ${ x } " ) " >>" ${ temporary } / ${ channel } .txt "
2025-06-09 15:07:05 +00:00
if [ [ ${ enablecsv } = "1" ] ] ; then
2025-06-24 13:44:20 -06:00
jq -c '[.upload_date, .timestamp, .duration, .uploader , .title, .webpage_url]' " ${ x } " | while read -r i; do
2025-06-24 15:25:48 -06:00
echo " ${ i } " | sed -e "s/^\[//g" -e " s/\] $//g " -e "s/\\\\\"/" /g" >>" ${ tmpcsv } "
2025-06-09 15:07:05 +00:00
done
fi
2025-06-11 14:17:03 +00:00
if [ [ ${ enabledb } = "1" ] ] ; then
2025-06-11 00:11:24 +00:00
jq -c '[.upload_date, .timestamp]' " ${ x } " | while read -r i; do
echo " ${ i } , ${ x ##*/ } " | sed -e "s/^\[//g" -e "s/\],/,/g" -e "s/\\\\\"/" /g" >>" ${ sortcsv } "
2025-06-09 15:07:05 +00:00
done
fi
2025-06-11 00:11:24 +00:00
echo " ${ count } / ${ total } ${ x } "
2025-06-09 05:07:30 +00:00
fi
2025-06-09 15:07:05 +00:00
) &
if [ [ $( jobs -r -p | wc -l) -ge $( getconf _NPROCESSORS_ONLN) ] ] ; then
wait -n
2025-05-30 16:09:56 +00:00
fi
2025-06-09 15:07:05 +00:00
2025-05-30 15:52:49 +00:00
done
wait
2025-06-11 14:17:03 +00:00
sleep 1
2025-06-02 14:21:59 +00:00
if [ [ ${ enabledb } = "1" ] ] ; then
2025-06-13 20:16:04 +00:00
sort " ${ sortcsv } " | uniq >" ${ temporary } / ${ channel } -sort-ordered.csv "
if [ [ -f " ${ temporary } / ${ channel } .db " ] ] ; then
rm " ${ temporary } / ${ channel } .db "
2025-06-09 15:07:05 +00:00
fi
2025-06-13 20:16:04 +00:00
echo " {\"playlistName\":\" ${ channel } \",\"protected\":false,\"description\":\"Videos from ${ channel } to watch later\",\"videos\":[ " >" ${ temporary } / ${ channel } .db "
2025-06-11 00:11:24 +00:00
count = 0
2025-06-13 20:16:04 +00:00
total = $( wc -l <" ${ temporary } / ${ channel } -sort-ordered.csv " )
2025-06-02 14:21:59 +00:00
while read -r line; do
2025-06-11 00:11:24 +00:00
count = $(( count + 1 ))
2025-06-02 14:21:59 +00:00
file = $( echo " ${ line } " | cut -d ',' -f3-)
2025-06-11 00:11:24 +00:00
if [ [ -f " ${ file } " ] ] ; then
2025-06-16 14:42:44 +00:00
jq -c " {\"videoId\": .id, \"title\": .title, \"author\": .uploader, \"authorId\": .channel_id, \"lengthSeconds\": .duration, \"published\": ( .timestamp * 1000 ), \"timeAdded\": $( date +%s) $( date +%N | cut -c-3) , \"playlistItemId\": \" $( cat /proc/sys/kernel/random/uuid) \", \"type\": .media_type} " " ${ temporary } / ${ file } " >>" ${ temporary } / ${ channel } .db "
2025-06-13 20:16:04 +00:00
echo "," >>" ${ temporary } / ${ channel } .db "
2025-06-11 00:11:24 +00:00
echo " ${ count } / ${ total } ${ file } "
2025-06-02 14:21:59 +00:00
fi
2025-06-13 20:16:04 +00:00
done <" ${ temporary } / ${ channel } -sort-ordered.csv "
echo " ],\"_id\":\" ${ channel } $( date +%s) \",\"createdAt\": $( date +%s) ,\"lastUpdatedAt\": $( date +%s) } " >>" ${ temporary } / ${ channel } .db "
2025-06-02 14:21:59 +00:00
rm " ${ json } "
2025-06-13 20:16:04 +00:00
grep -v -e ":[ ]*null" " ${ temporary } / ${ channel } .db " | tr '\n' '\r' | sed -e "s/,\r[,\r]*/,\r/g" | sed -e "s/,\r\]/\]/g" -e "s/\[\r,/\[/g" | tr '\r' '\n' | jq -c . >" ${ json } " && rm " ${ temporary } / ${ channel } .db "
rm " ${ temporary } / ${ channel } -sort-ordered.csv " " ${ sortcsv } "
2025-06-02 14:21:59 +00:00
fi
if [ [ ${ enablecsv } = "1" ] ] ; then
2025-06-24 15:25:48 -06:00
sort " ${ tmpcsv } " | uniq >" ${ temporary } / ${ channel } -without-header.csv "
echo '"Upload Date", "Timestamp", "Duration", "Uploader", "Title", "Webpage URL"' >" ${ temporary } / ${ channel } -tmp.csv "
cat " ${ temporary } / ${ channel } -without-header.csv " >>" ${ temporary } / ${ channel } -tmp.csv "
mv " ${ temporary } / ${ channel } -tmp.csv " " ${ csv } "
2025-06-13 20:16:04 +00:00
rm " ${ temporary } / ${ channel } -without-header.csv "
2025-06-24 15:25:48 -06:00
rm " ${ tmpcsv } "
2025-06-02 14:21:59 +00:00
fi
2025-06-11 00:11:24 +00:00
cd " ${ temporary } " || exit
2025-06-11 14:42:49 +00:00
tar -cvp -I "zstd -T0" -f " ${ subfolder } / ${ channel } .tar.zst " -- *.info.json
2025-06-12 14:02:10 +00:00
count = 0
total = $( find " ${ temporary } " -type f -iname "*.info.json" | wc -l)
2025-06-13 20:16:04 +00:00
sort " ${ temporary } / ${ channel } .txt " | uniq >" ${ archive } "
2025-06-11 14:42:49 +00:00
rm -rf " ${ temporary } "