Archive for the ‘Fedora’ Category

#!/bin/bash
# Procedure to copy mp3 files from Youtube or Soundcloud playlist to a smartphone
# Prerequisites: Ubuntu 20.04, Debian 10 or newer, bash shell, detox, 
# Prerequisites: aacgain, mp3gain, pip3, python, snap, parallel (to use multi-core processors)
# Prerequisites: Google Chrome Web browser in Debian 10, 
# Prerequisites: AirDroid on Android smartphone, 
# Prerequisites: Cloud Music Player - Listener on Apple iPhone
# Prerequisites: Only ports to keep open for this are the
# DNS or DNS-over-HTTPS port, port 80 and port 443
# Author: Mark Rijckenberg
# Last modification date: 2021/3/26

echo -n "Enter Youtube/Soundcloud playlist URL to convert to mp3 files: " 
read URL

echo -n "Enter full path where mp3 files should be stored: " 
read INSTALLDIR

# update contents of software repositories:
sudo dnf update
sudo DEBIAN_FRONTEND=noninteractive apt update

# select right tool for the job 
DETECTSOUNDCLOUD=`echo $URL | grep soundcloud | wc -l`
echo $DETECTSOUNDCLOUD

if [ $DETECTSOUNDCLOUD -gt 0 ] ; then
    TOOL=scdl
    echo $TOOL
  else
     TOOL=youtube-dl
     echo $TOOL
fi

# install snap (if not installed)
if ! type "snap" > /dev/null; then
sudo DEBIAN_FRONTEND=noninteractive apt install --yes --force-yes  snapd || sudo dnf install snapd
  else
  echo "snap installed"
fi

# install pip3 (if not installed)
if ! type "pip3" > /dev/null; then
sudo DEBIAN_FRONTEND=noninteractive apt install --yes --force-yes  python3-pip || sudo dnf install python3-pip
  else
  echo "pip3 installed"
fi

# install python (if not installed)
if ! type "python" > /dev/null; then
sudo DEBIAN_FRONTEND=noninteractive apt install --yes --force-yes  python-is-python3 || sudo dnf install python-is-python3
  else
  echo "python installed"
fi

# install detox (if not installed)
if ! type "detox" > /dev/null; then
sudo DEBIAN_FRONTEND=noninteractive apt install --yes --force-yes  detox || sudo dnf install detox
  else
  echo "detox installed"
fi

# install ffmpeg (if not installed)
if ! type "ffmpeg" > /dev/null; then
sudo DEBIAN_FRONTEND=noninteractive add-apt-repository --yes ppa:flexiondotorg/audio
sudo DEBIAN_FRONTEND=noninteractive apt update
sudo DEBIAN_FRONTEND=noninteractive apt install --yes --force-yes   ffmpeg || sudo dnf install ffmpeg
  else
  echo "ffmpeg installed"
fi

# install lame (if not installed)
if ! type "lame" > /dev/null; then
sudo DEBIAN_FRONTEND=noninteractive apt install --yes --force-yes  lame || sudo dnf install lame
  else
  echo "lame installed"
fi

# install mp3gain (if not installed)
if ! type "mp3gain" > /dev/null; then
sudo snap install mp3gain || sudo apt install mp3gain || sudo dnf install mp3gain
  else
  echo "mp3gain installed"
fi

# install aacgain (if not installed)
if ! type "aacgain" > /dev/null; then
sudo snap install aacgain || sudo apt install aacgain || sudo dnf install aacgain
  else
  echo "aacgain installed"
fi

# install parallel (if not installed)
if ! type "parallel" > /dev/null; then
sudo DEBIAN_FRONTEND=noninteractive apt install --yes --force-yes   parallel || sudo dnf install parallel
  else
  echo "parallel installed"
fi

if [ $DETECTSOUNDCLOUD -gt 0 ] ; then

# Soundcloud specific:
echo $TOOL
mkdir $INSTALLDIR
cd $INSTALLDIR
pip uninstall scdl 
pip3 uninstall scdl 
sudo rm /usr/local/bin/scdl
pip3 install git+https://github.com/flyingrub/scdl
sudo cp $HOME/.local/bin/scdl  /usr/local/bin/scdl
sudo cp $HOME/.local/bin/scdl  /usr/bin/scdl
$TOOL --addtofile -c -l $URL

  else
  
# Youtube specific:
echo $TOOL
mkdir $INSTALLDIR
rm $INSTALLDIR/$TOOL

# install/upgrade youtube-dl to newest version
# use pip instead of wget if wget command fails:
sudo -H pip install --upgrade youtube-dl || sudo wget https://yt-dl.org/downloads/latest/youtube-dl -O /usr/bin/youtube-dl
sudo chmod a+rx /usr/bin/youtube-dl
sudo chmod a+rx /usr/local/bin/youtube-dl
youtube-dl --version

cd $INSTALLDIR
PLAYLISTNAME=`$TOOL --flat-playlist --no-check-certificate  $URL | egrep -v "just" | egrep "Downloading playlist" | head -n1 | cut -d":" -f2`
PLAYLISTDIR=$(echo $PLAYLISTNAME | tr -d ' '| tr -d '&')
mkdir $INSTALLDIR/$PLAYLISTDIR
cd $INSTALLDIR/$PLAYLISTDIR
$TOOL --postprocessor-args "-threads 6" --restrict-filenames -o '%(title)s.%(ext)s' --no-check-certificate -v  --extract-audio --audio-format mp3 -i  $URL

fi

# rename problematic filenames using detox utility
detox -r $INSTALLDIR

# normalize volume (run 3 times in case mp3gain skips treatment of files during first 2 attempts)
cd $INSTALLDIR
find . -type f | parallel -X "xargs /snap/bin/mp3gain -r -T"
find . -type f | parallel -X "xargs /snap/bin/mp3gain -r -T"
find . -type f | parallel -X "xargs /snap/bin/mp3gain -r -T"

find . -type f | parallel -X "xargs mp3gain -r -T"
find . -type f | parallel -X "xargs mp3gain -r -T"
find . -type f | parallel -X "xargs mp3gain -r -T"

# normalize volume (run 3 times in case aacgain skips treatment of files during first 2 attempts)
find . -type f | parallel -X "xargs /snap/bin/aacgain -r "
find . -type f | parallel -X "xargs /snap/bin/aacgain -r "
find . -type f | parallel -X "xargs /snap/bin/aacgain -r "

find . -type f | parallel -X "xargs aacgain -r "
find . -type f | parallel -X "xargs aacgain -r "
find . -type f | parallel -X "xargs aacgain -r "


echo "Temporarily disable all extensions in Google Chrome webbrowser and reboot your Android smartphone before attempting the .mp3 file transfer from Ubuntu to Android via AirDroid"
echo "The Mozilla Firefox web browser is not (yet) compatible with AirDroid. You have to use Google Chrome."