There were issues with the last script I posted in terms of it working with an upload script that adds files to a folder gradually. Jaques kindly posted the following solution on Apple Discussions:
-
Create a subdirectory with the name “mp3-192kbs” in “this_folder”
-
A bash script:
#!/bin/bash
convertPath="/opt/local/bin/ffmpeg"
currDir=${1%/*}
for f in "$@";do
var2=$(/usr/bin/stat -f "%z" "$f");var1=0
while [ $var2 -gt $var1 ];do
var1=$var2
/bin/sleep 12
## get file size, if var2 is greater than var1 --> downloading ##
var2=$(/usr/bin/stat -f "%z" "$f")
done
## if ffmpeg is running by another script, wait until ffmpeg exit ##
while /bin/ps -Ax| /usr/bin/grep $convertPath | /usr/bin/grep -v grep; do
/bin/sleep 15
done
filename=${f##*/}; base=${filename%.*}; i=""
## add number if the mp3 already exists in subfolder ##
while [ -e "$currDir/mp3-192kbs/$base$i.mp3" ];do
((i++))
done
##convert if no error delete original file ##
$convertPath -i "$f" -f mp3 -ar 44100 -ab 192k -acodec libmp3lame "$currDir/mp3-192kbs/$base$i.mp3" -map_meta_data "$f":"$currDir/mp3-192kbs/$base$i.mp3" && /bin/rm -f "$f"
done
Saved in the folder “/opt/local/bin/” with the name “ConvertToMp3.sh”
- enter the following text in terminal:
chmod u+x /opt/local/bin/ConvertToMp3.sh
and press return
- The folder action script:
on adding folder items to this_folder after receiving added_items
set this_folderPOSIX to POSIX path of this_folder
set convertPath to "/opt/local/bin/ffmpeg"
set bashScript to quoted form of "/opt/local/bin/ConvertToMp3.sh"
set validFiles to ""
repeat with aFile in added_items
try
set t_ext to name extension of (info for aFile without size)
on error
set t_ext to missing value
end try
if t_ext is not missing value then
if t_ext is in {"m4a", "ogg", "wav", "flac", "aiff", "mp3", "m4p"} then
set tPath to (quoted form of POSIX path of aFile)
if t_ext is "mp3" then
try
do shell script convertPath & " -i " & tPath & " /dev/null 2>&1 | /usr/bin/grep -m1 bitrate | /usr/bin/sed 's/.*bitrate://'"
if (word 1 of the result) as integer > 192 then
set validFiles to validFiles & tPath & " "
end if
end try
else
set validFiles to validFiles & tPath & " "
end if
else if t_ext is in {"nfo", "txt"} then
try
tell application "Finder" to delete aFile
end try
end if
end if
end repeat
-- run shell in background, this Applescript exit without waiting the shell
if validFiles is not "" then do shell script bashScript & space & validFiles & " > /dev/null 2>&1 &"
end adding folder items to
This appears to be working fine, but depends on the creation of the folder ‘mp3-192kbs’, which won’t work for my purposes. Here is the process that I am trying to set up:
Files are uploaded by a user to a webpage. This webpage creates a folder for each user (using PHP) in the parent directory ‘songs’, based on their login username, and then uploads the audio files to this folder. I would like the folder action to be run on all ‘name’ subfolders in the parent directory. This method of uploading/converting files raises the following issues:
-
The script above depends on a subfolder called ‘MP3-192kbs’ in any folder it is attached to. I am wondering if there is a way that the script could be adapted to create (converted) files in the same folder as the script is attached to? This is because the PHP script that generates the playlist depends on the above directory structure.
-
The ‘name’ folders that are created dynamically have write permissions for the user _www, which is the default Apache user. Because of this a folder action script can’t write any converted files to these folders, as the default user on my Mac, ‘nick’, doesn’t have write permissions. So I presume this means that I need to find a way of running the folder action as the user _www, or perhaps a way of changing the permissions for any folders added to the directory ‘songs’ (with a folder action?)
-
Because the name folders are created dynamically I need to find a way of either dynamically attaching the folder actions (with a folder action on the ‘songs’ directory?), or amending the above folder action so that it can run on subdirectories too (I am not sure if this can be done with folder actions).
I hope this is clear. I asked another question about the permissions issue on Apple Discussions and it was suggested that I could use launchd, which could possibly be used to run folder actions as _www and dynamically add folder actions, but I’m not sure, and this would mean learning a new language for a website that will be going live on Friday!
If anyone has any thoughts/suggestions on any of the above points I’d be glad to hear them,
Thanks,
Nick