Page 1 of 1

Passing variables to bash script - problems

PostPosted: Fri Jan 24, 2014 6:38 am
by musecaptain
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?

Re: Passing variables to bash script - problems

PostPosted: Fri Jan 24, 2014 4:36 pm
by Mr_Noodle
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.

Re: Passing variables to bash script - problems

PostPosted: Sat Mar 05, 2016 7:20 pm
by tomar012
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.

Re: Passing variables to bash script - problems

PostPosted: Mon Mar 07, 2016 11:50 am
by Mr_Noodle
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.

Re: Passing variables to bash script - problems

PostPosted: Fri Mar 11, 2016 4:13 pm
by tomar012
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