I wanted to download videos (or more precisely I was only interested in the audio) from youtube. Unfortunately, some videos had more intro and outro than just the music I wanted. Fortunately ffmpeg in combination with youtube-dl and some shell scripting is the key to solve the problem:
#!/bin/bash#!/bin/bash
# massively inspired from https://github.com/rg3/youtube-dl/issues/622 & man youtube-dl & man ffmpeg & google
# loop over download-links.csv, with a semicolon as delimiter.
# Columns with CAPITALS are actually needed, others only to calculate start (SS) and duration (T)
IFS=";"
while read TITLE URL start end START_TIME END_TIME
do
if [[ "$URL" == "http"* ]]; then
echo "### Downloading $TITLE:"
FILENAME="$TITLE"
EXT="mp3"
AUTHOR="artist"
ALBUM="album"
# download audio only from START_TIME for duration END_TIME
# also write id3 tags. Currently aritst and album is hardcoded
ffmpeg \
-n \
-nostdin \
-i $(youtube-dl -f 22 --get-url $URL) \
-ss $START_TIME \
-t $END_TIME \
-c:a libmp3lame -qscale:a 2 \
-metadata title="$TITLE" \
-metadata author="$AUTHOR" \
-metadata album="$ALBUM" \
"$FILENAME.$EXT"
# finally check the file and id3 tag
ffprobe "$FILENAME.$EXT" 2>&1 | grep -A90 'Metadata:'
fi
done < download-links.csv
# alternatively download video
#ffmpeg -ss $START_TIME -i $(youtube-dl -f 22 --get-url $URL) -t $END_TIME -c:v copy -c:a copy "$FILENAME.$EXT"
All you need is to create a csv-file following the following structure:
| Title | URL | Start-Time | End-Time | Start | Duration |
| bla | https://www.youtube.com/watch?v=….. | 00:00:12 | 00:04:04 | 00:00:12 | 00:03:52 |