I just realized a wonderful feature in Youtube-dl: ytsearch:$SEARCHTERM. If you lack the context, scroll down.
I am using Nextcloud Notes and youtube-dl for automated downloads of Youtube-Videos that I want to watch later or for Music that I autoconvert to MP3s and put them in playlists (yeah I am old-fashioned, I still have tons of Vinyl). So if you enter youtube-dl ytsearch:Feilner, this gem will download the first video found – which is my presentation about Mythbusting documentation:
mfeilner@fibonacci:~> youtube-dl ytsearch:Feilner
[youtube:search] query “Feilner”: Downloading page 1
[download] Downloading playlist: Feilner
[youtube:search] playlist Feilner: Collected 1 video ids (downloading 1 of them)
[download] Downloading video 1 of 1
[youtube] Cpug8Iqw3f8: Downloading webpage
[youtube] Cpug8Iqw3f8: Downloading video info webpage
[youtube] Cpug8Iqw3f8: Downloading MPD manifest
[dashsegments] Total fragments: 376
[download] Destination: Mythbusting Documentation – Markus Feilner-Cpug8Iqw3f8.f137.mp4
[download] 10.6% of ~323.89MiB at 2.59MiB/s ETA 02:07
After a few minutes, you’ll find the mp4 in your working directory. Guess I have to experiment with that more thoroughly… Update: I just found the beautiful option to tell ytsearch: how many files it should retrieve from the search: youtube-dl ytsearch10:Feilner will download the top ten video files from the search result, ytsearch20 twenty.
But what I also do is: I am using Nextcloud Notes to add music, video, and other stuff that I want downloaded on a local machine. I can add links and stuff to this .txt file from my smartphone or all my machines and laptops. Have a look at Notes, it’s awesome. Only one caveat: First line is always the name of the file. Markup is supported, but I don’t need that. Here’s my download-script that omits the first line and does some path magic – you can see I am not a coder, but it works like a charm.
#!/bin/bash
# download Youtube videos from a list in a txt file and call scripts to convert them
IFS=”¬”
if [ -z $1 ]
then
INPUT=/home/mfeilner/Nextcloud/Notes/Youtube.txt
else
INPUT=$1
fi
echo $INPUT
INFILE=/tmp/infile
awk ‘NR>1’ $INPUT > $INFILE
TEMPDIR=~/Temp/ytd/`date +%Y-%m-%d-%H-%M`
mkdir -p $TEMPDIR
cd $TEMPDIR
while IFS= read -r line
do
youtube-dl “”$line””
done < “$INFILE”
/home/mfeilner/Nextcloud/config/bin/mp42mp3
mkdir MP3; mv *.mp3 MP3/; cd MP3
/home/mfeilner/Nextcloud/config/bin/mkplaylist $TEMPDIR
mp42mp3 converts lots of video formats (it will be improved, yes), and mkplaylist creates m3u files. I also have a mp3tidy script that cleans up filenames by normalizing them and does some basic id3tagging.
#!/bin/bash
# convert media formats
IFS=”¬”
for file in *mp4
do ffmpeg -i “$file” `basename -s mp4 $file`mp3;
done
for file in *webm
do ffmpeg -i “$file” `basename -s webm $file`mp3;
done
for file in *mkv
do ffmpeg -i “$file” `basename -s mkv $file`mp3;
done
for file in *m4a
do ffmpeg -i “$file” `basename -s m4a $file`mp3;
done
I have some more scripts running that tidy up filenames and tags and stuff, but they are too embarassing to publish (yet).
For those that lack the context: Youtube-dl is a wonderful Video downloader for youtube. It also supports direct encoding to mp3, but I prefer to have hands on myself if I need it, that’s why ffmeg comes in handy. And because of the nasty first line in Youtube.txt, I decided to not use Youtube-dls read-from-file mechanism.
youtube-dl https://www.youtube.com/watch?v=Cpug8Iqw3f8
downloads the Feilner video from the example above. Ffmpeg could encode it to mp3 so that there’s only the audio, creating a podcast e.g. for the car. Rename would help stripping unwanted stuff from the filename and there’s some tools out there to change the tags properly.
To get rid of the Youtube-ID in the filename and for basic id3tagging I do this:
#!/bin/bash
# normalize Youtube-Downloaded MP3 files after conversion from Mkv or similar,
# ID3-Tag them by adding filename to Artist, Title and Album fields.
IFS=”¬”
INFILE=/tmp/infile
OUTFILE=/tmp/outfile
TEMPDIR=~/Temp/ytd/date +%Y-%m-%d-%H-%M
FILENAME=/tmp/filename
CHARCOUNT=17
ls *mp3 -1 > $INFILE
mkdir -p $TEMPDIR
while IFS=”²” read -r line
do
echo “”$line”” | rev |cut -c $CHARCOUNT- | rev > $FILENAME
mv “”$line”” $TEMPDIR/`cat $FILENAME`.mp3
id3tag -a=`cat $FILENAME` -A=`cat $FILENAME` -s=`cat $FILENAME`
done <$INFILE
mv $TEMPDIR ~/Downloads/New
rm $OUTFILE $INFILE $FILENAME