Download music from youtube with batch file - bash

I made a script for downloading music from Youtube using youtube-dl. It worked fine until an update of some kind.
#!/bin/bash
read -p ' Title : ' title
read -p 'Artist : ' artist
read -p ' Album : ' album
read -p ' Genre : ' genre
youtube-dl "$1" --quiet --extract-audio --audio-format mp3 --audio-quality 3 --embed-thumbnail --exec 'mp3gain -q -r -c -s i {} > /dev/null & echo -n {}' | xargs -0 mid3v2 -t "$title" -a "$artist" -A "$album" -g "$genre"
echo --- DONE! ---
My beleive is that it is the mp3gain program that is causing the problem and I don't really care about it. I've tried to change the --exec line to:
--exec mid3v2 -t "$title" -a "$artist" -A "$album" -g "$genre"
with no luck.
I liked it because with the command:
"download.bat youtube-url"
I got an mp3 with the sound and a thumbnail.
How could I make a change for it to work?

You are almost close. Would you please change the youtube-dl line as:
youtube-dl "$1" --quiet --extract-audio --audio-format mp3 --audio-quality 3 --embed-thumbnail --exec 'mp3gain -q -r -c -s i {} > /dev/null 2>&1 && printf "%s\0" {}' | xargs -0 mid3v2 -t "$title" -a "$artist" -A "$album" -g "$genre"
mp3gain outputs some messages via stderr. You will need to suppress
them with 2>&1.
It is a good practice to add -0 option to xargs. Then you need to
terminate the filename with a null character.

Related

How to do auto upgrade to latest github software release version?

Releases uploads every time to url like
https://github.com/ipfs/go-ipfs/releases/tag/v0.9.1
my script is
#!/bin/bash
rm /home/ipfs/go-ipfs -rf
rm go-ipfs.tar.gz
curl -s https://api.github.com/repos/ipfs/go-ipfs/releases/latest | grep linux-amd64.tar.gz\" | grep download | sed 's/.*: \"//g' | sed 's/\"//g' | wget -i - -O /home/ipfs/go-ipfs.tar.gz
if test -f /home/ipfs/go-ipfs.tar.gz then
tar -xf /home/ipfs/go-ipfs.tar.g
newsize=$(wc -c <"/home/ipfs/go-ipfs/ipfs")
cursize=$(wc -c <"/home/ipfs/ipfs")
if [$newsize -ne $cursize]; then
mv /home/ipfs/go-ipfs/ipfs /home/ipfs/ipfs
chmod +x /home/ipfs/ipfs
pkill ipfs
fi
fi
but it has an error i cant fix
Solution is
#!/bin/bash
#remove old repo folder
rm /home/ipfs/go-ipfs -rf
#remove old tar.gz
rm go-ipfs.tar.gz
#try to download new
curl -s https://api.github.com/repos/ipfs/go-ipfs/releases/latest | grep linux-amd64.tar.gz\" | grep download | sed 's/.*: \"//g' | sed 's/\"//g' | wget -i - -O /home/ipfs/go-ipfs.tar.gz
#check file exists
if [ -f /home/ipfs/go-ipfs.tar.gz ]; then
#unpack tar gz
tar -xf /home/ipfs/go-ipfs.tar.gz
#get file sizes
newsize=$(wc -c <"/home/ipfs/go-ipfs/ipfs")
cursize=$(wc -c <"/home/ipfs/ipfs")
#if new file is not as current
if (($newsize != $cursize)); then
#replace it
mv /home/ipfs/go-ipfs/ipfs /home/ipfs/ipfs
chmod +x /home/ipfs/ipfs
#kill old to restart new
pkill ipfs
fi
fi

CPU optimization for gif wallpaper

I'm using xubuntu with xfc4, for pure fun I tried to make a script which allows me to put a gif as a desktop wallpaper because this is not a default feature in xfce4.
I've already made a script which seem to work pretty fine excepted that the CPU get in a great trouble with it. So is there a way to optimize this code to do the same thing but stay friend with my CPU?
BG_GIF=/home/grasteau/Pictures/walpapper.gif
DURATION=$(exiftool -Duration walpapper.gif | sed 's/ //g' | sed 's/Duration://g' | sed 's/s//g')
PATH_IMAGE=/
mkdir -p /dev/shm/background
rm -f /dev/shm/background/*
gm convert $BG_GIF +adjoin /dev/shm/background/target%d.png
NUMBER_OF_FRAME=$(bc <<< "$(ls -1 /dev/shm/background | wc -l) - 1")
DELAY=$(bc <<< "scale=3; $DURATION/$NUMBER_OF_FRAME")
while true
do
for i in $(seq 0 $NUMBER_OF_FRAME);
do
PATH_IMAGE="/dev/shm/background/target$i.png"
xfconf-query -c xfce4-desktop -l | grep "last-image$" | while read -r line
do
xfconf-query -c xfce4-desktop -p $line -s $PATH_IMAGE
done
sleep $DELAY
done
done

Bash: Parse Urls from file, process them and then remove them from the file

I am trying to automate a procedure where the system will fetch the contents of a file (1 Url per line), use wget to grab the files from the site (https folder) and then remove the line from the file.
I have made several tries but the sed part (at the end) cannot understand the string (I tried escaping characters) and remove it from that file!
cat File
https://something.net/xxx/data/Folder1/
https://something.net/xxx/data/Folder2/
https://something.net/xxx/data/Folder3/
My line of code is:
cat File | xargs -n1 -I # bash -c 'wget -r -nd -l 1 -c -A rar,zip,7z,txt,jpg,iso,sfv,md5,pdf --no-parent --restrict-file-names=nocontrol --user=test --password=pass --no-check-certificate "#" -P /mnt/USB/ && sed -e 's|#||g' File'
It works up until the sed -e 's|#||g' File part..
Thanks in advance!
Dont use cat if it's posible. It's bad practice and can be problem with big files... You can change
cat File | xargs -n1 -I # bash -c
to
for siteUrl in $( < "File" ); do
It's be more correct and be simpler to use sed with double quotes... My variant:
scriptDir=$( dirname -- "$0" )
for siteUrl in $( < "$scriptDir/File.txt" )
do
if [[ -z "$siteUrl" ]]; then break; fi # break line if him empty
wget -r -nd -l 1 -c -A rar,zip,7z,txt,jpg,iso,sfv,md5,pdf --no-parent --restrict-file-names=nocontrol --user=test --password=pass --no-check-certificate "$siteUrl" -P /mnt/USB/ && sed -i "s|$siteUrl||g" "$scriptDir/File.txt"
done
#beliy answers looks good!
If you want a one-liner, you can do:
while read -r line; do \
wget -r -nd -l 1 -c -A rar,zip,7z,txt,jpg,iso,sfv,md5,pdf \
--no-parent --restrict-file-names=nocontrol --user=test \
--password=pass --no-check-certificate "$line" -P /mnt/USB/ \
&& sed -i -e '\|'"$line"'|d' "File.txt"; \
done < File.txt
EDIT:
You need to add a \ in front of the first pipe
I believe you just need to use double quotes after sed -e. Instead of:
'...&& sed -e 's|#||g' File'
you would need
'...&& sed -e '"'s|#||g'"' File'
I see what you trying to do, but I dont understand the sed command including pipes. Maybe some fancy format that I dont understand.
Anyway, I think the sed command should look like this...
sed -e 's/#//g'
This command will remove all # from the stream.
I hope this helps!

wget to display filename and percentage downloaded

I am trying to use wget in a bash and display a custom download percentage per file so that the user knows the process is running and what has been downloaded. The below seems to download the files but there is no percentage or filename being downloaded displayed. I am not sure why and cannot seem to figure it out. Thank you :).
list
xxxx://www.xxx.com/xxx/xxxx/xxx/FilterDuplicates.html
xxxx://www.xxx.com/xxx/xxxx/xxx/file1.bam
xxxx://www.xxx.com/xxx/xxxx/xxx/file2.bam
xxxx://www.xxx.com/xxx/xxxx/xxx/file1.vcf.gz
xxxx://www.xxx.com/xxx/xxxx/xxx/file2.vcf.gz
bash that uses list to download all fiiles
# download all from list
download() {
local url=$1
echo -n " "
wget --progress=dot $url 2>&1 | grep --line-buffered "%" | sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
echo -ne "\b\b\b\b"
echo " starting download"
}
cd "/home/user/Desktop/folder/subfolder"
wget -i /home/cmccabe/list --user=xxx--password=xxx --xxx \
xxxx://www.xxx.com/xxx/xxxx/xxx/ 2>&1 -o wget.log | download

Bash piping issue

I need to execute the following grep query as an argument for konsole (the kde terminal)
grep -R -i -n -A 2 -B 2 --color=always -R "searchtext" * | less -R
works for the current terminal.
konsole --workdir `pwd` -e grep -R -i -n -A 2 -B 2 --color=always -R "searchtext" * | less -R
works, but the konsole window displays the grep query without less pipe.
Ideally I want konsole to spawn as seperate process with konsole &
and send the grep command with less as an argument for konsole -e
You need to run the pipe in a shell.
konsole --workdir pwd -e bash -c 'grep -R -i -n -A 2 -B 2 --color=always -R "searchtext" * | less -R'

Resources