Passing variables to bash script - problems

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Hi,

I'm trying to build a watch folder for FFMPEG that compress files and move them to a specific destination folder. Using this command line:
ffmpeg -y -i $1 -vcodec libx264 -b:v 2M -s 1280x720 -pix_fmt yuv420p -b:a 192k /Volumes/EncodingServer/Output/_MP4/$1.mp4

Which results in this command

ffmpeg -y -i /Volumes/EncodingServer/Input/_MP4/Test.mov -vcodec libx264 -b:v 2M -s 1280x720 -pix_fmt yuv420p -b:a 192k /Volumes/EncodingServer/Output/_MP4//Volumes/EncodingServer/Input/_MP4/Test.mov.mp4

Naturally this errors out.

The problems are:
1. $1 is used as a variable for the matched file(s) but include their paths (eg. /Volumes/Disk/Test.mov instead of Test.mov). Is it possible to only pass the file name so that this can be used to specify the output file name for ffmpeg?

2. Also how can I pass the matched file without the file extension (to get Test.mp4 instead of Test.mov.mp4

3. Also I noticed that when one has spaces in the matched files names, Hazel doesn’t seem to escape the file name (eg. turn "Test 1" into "Test\ 1"). Because of this ffmpeg errors out.

4. Finally I can't change the out file location dynamically using Hazel, but have to hardcode the path into the bash script. I assume this is not solvable?
musecaptain
 
Posts: 27
Joined: Tue Oct 08, 2013 2:38 pm

These are more general shell scripting questions but:

1. Look up "basename"
2. There is some way to do this but I don't recall off the top of my head. You'll have to google it.
3. You need to quote the variable ("$1")
4. It depends. I don't have enough info to answer this but you are given the file path as input so if it's something that can be keyed off of that, then yes.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

I have a similar problem. I can't seem to get Hazel (or Bash) to execute the TRUE condition of the following if statement.

filename=$(basename "$1")
filename=${filename%.*}
converting_directory="Data:Plex Media:Handbrake Encoding"
converting_filenameHD="$filename.m4v"

if mediainfo --Inform="Video;%ScanType%" $1=="Interlaced"; then
ffmpeg -i "$1" -c:v libx264 -preset slow -b 10000k -vf yadif -c:a copy "$converting_filenameHD"
else
ffmpeg -i "$1" -vcodec copy -acodec copy "$converting_filenameHD"
fi

This may be more of a Bash question than a Hazel question but I don't know how to troubleshoot Bash from within Hazel.
tomar012
 
Posts: 5
Joined: Sat Jun 21, 2014 9:06 pm

Try the script outside of Hazel. Add echo statements to output variables at different points in the script. Once you have it working then paste the script back into Hazel.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

I finally figured it out. Thanks for the tip Mr Noodle

filename=$(basename "$1")
filename=${filename%.*}
converting_directory="Data:Plex Media:Handbrake Encoding"
converting_filenameHD="$filename.m4v"

if [[ $(mediainfo --Inform="Video;%ScanType%" "$1") == "Interlaced" ]]
then
ffmpeg -i "$1" -c:v libx264 -preset slow -b 10000k -vf yadif -c:a copy "$converting_filenameHD"
else
ffmpeg -i "$1" -vcodec copy -acodec copy "$converting_filenameHD"
fi
tomar012
 
Posts: 5
Joined: Sat Jun 21, 2014 9:06 pm


Return to Support