Need help with applescript to convert audio files using ffmpeg

Discussion in 'Mac Basics and Help' started by nick_harambee, Sep 26, 2010.

  1. nick_harambee macrumors regular

    Joined:
    May 20, 2005
    #1
    Hi

    I have installed ffmpeg using MacPorts and am trying to work out a script that will convert audio files in a specified folder to mp3 files, outputting the converted files as mp3 files in another folder.

    So far the script looks like this:

    Code:
    set convertPath to "Macintosh HD:opt:local:bin:ffmpeg"
    set convertCommand to quoted form of POSIX path of convertPath & " -i {infile} -f mp3 -ar 44100 -ab 128 -acodec mp3 - "
    
    tell application "Finder"
    	set inPath to "Macintosh HD:Users:nick:Desktop:new:"
    	set outPath to "Macintosh HD:Users:nick:Desktop:new2:"
    	set myFiles to (files of entire contents of inPath whose name ends with ".m4a") as alias list
    	
    	repeat with i from 1 to count of myFiles
    		do shell script convertCommand
    	end repeat
    	
    end tell
    With this script I am getting the error : "Can’t make every file of «class ects» of "Macintosh HD:Users:nick:Desktop:new:" whose name ends with ".m4a" into type alias list."

    Another issue is that I am not sure how to define the do shell script command so that it outputs an MP3 file in the output directory.

    Could someone help me get this working?

    Thanks

    Nick
     
  2. mulo macrumors 68020

    mulo

    Joined:
    Aug 22, 2010
    Location:
    Behind you
    #2
    just download all2mp3, works great, just don't load more then 500 songs into it at once..
     
  3. nick_harambee thread starter macrumors regular

    Joined:
    May 20, 2005
    #3
    thanks, but if possible I'd like to do the conversion within a script as the script will be performing other functions as well.
     
  4. calderone macrumors 68040

    calderone

    Joined:
    Aug 28, 2009
    Location:
    Seattle
    #4
    First, switch to homebrew: http://wiki.github.com/mxcl/homebrew/installation

    It is much better than MacPorts.

    Second, you need to be using "as alias" on the in and out Paths. You are also making an error in specifiying the convert command when there are no files to operate on.

    There were some other errors as well, but here is a working script:

    Code:
    set convertPath to "Macintosh HD:opt:local:bin:ffmpeg"
    
    tell application "Finder"
    	set inPath to "Macintosh HD:Users:nick:Desktop:new" as alias
    	set outPath to "Macintosh HD:Users:nick:Desktop:new2" as alias
    	set filesToConvert to (name of every item of inPath whose name extension is "m4a")
    end tell
    
    repeat with i from 1 to count of filesToConvert
    	set fileName to removeExtension(item i of filesToConvert)
    	#display dialog fileName
    	do shell script POSIX path of convertPath & " -i " & (quoted form of ((POSIX path of inPath) & item i of filesToConvert)) & " -f mp3 -ar 44100 -ab 128k -acodec libmp3lame " & (quoted form of (POSIX path of outPath)) & quoted form of (fileName & ".mp3")
    end repeat
    
    to removeExtension(fileToParse)
    	#display dialog fileToParse
    	set prevTIDs to text item delimiters of AppleScript
    	set text item delimiters of AppleScript to "."
    	return first text item of fileToParse
    	set text item delimiters of AppleScript to prevTIDs
    end removeExtension
    
     
  5. nick_harambee thread starter macrumors regular

    Joined:
    May 20, 2005
    #5
    Thanks. That works great, with one remaining issue, the metadata isn't preserved in the resulting mp3 file.

    I found the following command line googling about and it includes -map_meta_data, which is supposed to preserve metadata, but when I try to add it to your command it generates an error:

    ffmpeg -i audio1.aac -ar 22050 -ab 32 -map_meta_data audio1.mp3:audio1.aac audio1.mp3

    regarding homebrew, presumably I should just keep my existing installations of ffmpeg and ImageMagick and do any subsequent compiling with homebrew, as it would perhaps be complicated to uninstall ffmpeg and ImageMagick. Having said this, if MacPorts installs everything in the /opt directory then perhaps I can just delete this directory and start again with homebrew?

    Nick
     
  6. calderone macrumors 68040

    calderone

    Joined:
    Aug 28, 2009
    Location:
    Seattle
    #6
    Code:
    do shell script POSIX path of convertPath & " -i " & (quoted form of ((POSIX path of inPath) & item i of filesToConvert)) & " -f mp3 -ar 44100 -ab 128k -acodec libmp3lame " & (quoted form of (POSIX path of outPath)) & quoted form of (fileName & ".mp3") & " -map_meta_data " & (quoted form of ((POSIX path of inPath) & item i of filesToConvert)) & ":" & (quoted form of (POSIX path of outPath)) & quoted form of (fileName & ".mp3")
    There is the new command. Of course, that is getting a little complicated. You could probably break all of that out into a function and it would be more efficient and manageable.

    As far as MacPorts vs homebrew goes, you can follow this guide: http://guide.macports.org/#installing.macports.uninstalling

    to get ride of MacPorts. Homebew is a much more elegant solution and generally doesn't require ay hacks the way MacPorts sometimes did.

    Of course, it is personal preference, but if Macports works for you, just stick with it.
     
  7. nick_harambee thread starter macrumors regular

    Joined:
    May 20, 2005
    #7
    Thanks. Do you know of any way of getting the comments and year tags from the iTunes Apple Lossless file to be copied over as well? At the moment I am getting a shortened version of comments and no year tag.
     
  8. calderone macrumors 68040

    calderone

    Joined:
    Aug 28, 2009
    Location:
    Seattle
    #8
    I do not. It is probably related to some non-standard way Apple is attaching those attributes. You would have to do some digging on that.
     
  9. nick_harambee thread starter macrumors regular

    Joined:
    May 20, 2005

Share This Page