0

I have a script called automateutube that I edit in VIM and execute in the terminal with sh ./automateutube.sh This script pulls youtube links from a file called songs.txt and downloads the video from youtube then extracts the audio.

The songs.txt file looks like this

https://www.youtube.com/watch?v=IxQOlZ3pqtI
https://www.youtube.com/watch?v=IxQOlZ3pqtI
https://www.youtube.com/watch?v=IxQOlZ3pqtI
https://www.youtube.com/watch?v=IxQOlZ3pqtI

It is just a bunch of links, one per line.

The script looks like this

#!/bin/bash

while read p; do    

x=/tmp/.youtube-dl-$(date +%y.%m.%d_%H.%M.%S)-$RANDOM.flv

youtube-dl --audio-quality 160k --output=$x --format=18 "$p"

ffmpeg -i $x -acodec libmp3lame -ac 2 -ab 128k -vn -y "$p"

rm $x

done <songs.txt

Now the first part executes. It downloads the video and starts to unpack it.

It is the second part that fails. ffmpeg -i $x -acodec libmp3lame -ac 2 -ab 128k -vn -y "$p"

This is because "$p" is supposed to be in format "filename.mp3" However as it is p takes the value of a youtube link, without ".mp3" appended.

This works for the first line

youtube-dl --audio-quality 160k --output=$x --format=18 "$p"

because "$p" is supposed to be in the form of a link there.

Now I have tried adding three lines in

a="$.mp3"
b="$p"
c=$b$a

and making ffmpeg -i $x -acodec libmp3lame -ac 2 -ab 128k -vn -y "$p"

into ffmpeg -i $x -acodec libmp3lame -ac 2 -ab 128k -vn -y "$c"

but I am still getting an error. Any ideas?

parse error, at least 3 arguments were expected, only 1 given in string 'om/watch?v=sOAHOxbMOJY'

Maxwell Chandler
  • 626
  • 8
  • 18

1 Answers1

0

So after some experimentation using advice from the comments, I came to this, which works.

#!/bin/sh

while read -r p; do    

x=/tmp/.youtube-dl-$(date +%y.%m.%d_%H.%M.%S)-$RANDOM.flv

youtube-dl --audio-quality 160k --output="$x" --format=18 "$p"

SUBSTRING=$(echo "$p"| cut -c33-50)

ffmpeg -i "$x" -acodec libmp3lame -ac 2 -ab 128k -vn -y "$SUBSTRING.mp3" </dev/null

rm "$x"

done <songs.txt

What this fixes is keeping /'s out of the file name, and eliminating parser error.

Maxwell Chandler
  • 626
  • 8
  • 18
  • 2
    You can replace the line `SUBSTRING=$(echo "$p"| cut -c33-50)` with `SUBSTRING="${p:32:18}"` to avoid creating a subshell that executes a command. – Jeffrey Cash Jan 29 '17 at 09:01