PDA

View Full Version : [all variants] [SOLVED] Small scripting question



whitethorn
October 14th, 2008, 12:13 PM
Hi,

I'm trying to write a script to dload youtube videos and convert them to .mp3. So far I'm able to dload the videos and convert them. My problem is I want to keep the title of the file. So when I run


youtube-dl -l "$1"
I'd like to save the title, so whatever is in front of the .flv in a variable to I can replace it after using ffmpeg and ecasound. I've found a couple other scripts but they weren't as automated as I wanted it to be (required me to enter a file name). I'm just stuck on reading the filename and saving it as a variable.


So I think I've figure it out, but there's probably a better way of getting this done. Any suggestions would be great. Here's my script



#!/bin/bash
youtube-dl -l "$1"
a=`ls |grep flv`
mv *.flv /tmp/test.flv
ffmpeg -i /tmp/test.flv -acodec mp3 -ac 2 /tmp/test.mp3
ecasound -i /tmp/test.mp3 -etf:8 -o ~/Desktop/"${a%flv}mp3"
rm /tmp/test.mp3
rm /tmp/test.flv

cmnorton
October 14th, 2008, 01:39 PM
Are you looking for basename, so you can separate the name of the file before the period, and use that somewhere else?

`basename $1`

and use that to attach to the front of ".mp3".

stephanvaningen
October 14th, 2008, 02:12 PM
If you would like the part "file" in "/folder/file.ext", you could maybe use this:

#!/bin/bash
# Copy-paste this in a file called i.e test.sh and do:
# sudo chmod u+x test.sh
# then run test.sh by running ./test.sh on the terminal when you're in the same directory as the file you created
file="/home/stephan/scripts/test.sh"
basename=${file##*/}
filepart=${basename%.*}
echo "file part is *$filepart*"

Is this what you were looking for?
(source: http://splike.com/wiki/Bash_Scripting_FAQ)

stephanvaningen
October 14th, 2008, 02:59 PM
Or easier:

basename $1 .mp3
which strips the ".mp3" from the basename


If you would like the part "file" in "/folder/file.ext", you could maybe use this:

#!/bin/bash
# Copy-paste this in a file called i.e test.sh and do:
# sudo chmod u+x test.sh
# then run test.sh by running ./test.sh on the terminal when you're in the same directory as the file you created
file="/home/stephan/scripts/test.sh"
basename=${file##*/}
filepart=${basename%.*}
echo "file part is *$filepart*"

Is this what you were looking for?
(source: http://splike.com/wiki/Bash_Scripting_FAQ)

whitethorn
October 14th, 2008, 03:21 PM
That's what I was looking for thanx.