Applescript: How to save text2speech and convert to mp3?
The say
command has been able to save in different formats for a while. The supported formats can vary with the voice, but for Mei-Jia
it will save to mp3
or mp4
directly, so you don't need to do any converting or deleting.
EDIT:
Looks like I was mistaken about the encoding - there isn't an mp3
, but you can use the say
shell script to set other encodings such as mp4
:
on run {input, parameters}
set saveFolder to (((path to music folder) as text) & "Mandarin-text2speech:")
repeat with thisText in input
set fileName to getUniqueName for "audio.mp4" from saveFolder
set output to quoted form of POSIX path of (saveFolder & fileName)
do shell script "say -v 'Mei-Jia' -r 130 -o " & output & " --file-format=mp4f " & quoted form of thisText
end repeat
end run
to getUniqueName for someName from someFolder -- add a numerical suffix as needed to get a unique name
set {divider, counter} to {"_", 0} -- the dividing text and starting suffix number
set leadingZeros to 2 -- maximum leading zeros
set here to -(offset of "." in ((reverse of text items of someName) as text)) - 1 -- split extension at last period
set theName to text 1 thru here of someName
if here is -1 then -- no extension
set theExtension to ""
else
set theExtension to text (here + 1) thru -1 of someName
end if
if counter < 1 then -- always start with a suffix
set counter to 1
if leadingZeros > 0 then set counter to text -(leadingZeros + 1) thru -1 of ("000000" & counter)
set newName to theName & divider & counter & theExtension
else -- only add suffix as needed
set counter to counter - 1 -- adjust for while loop
set newName to theName & theExtension
end if
tell application "System Events" to tell (get name of items of folder (someFolder as text) whose visible is true)
repeat while it contains newName
set counter to counter + 1
if leadingZeros > 0 then set counter to text -(leadingZeros + 1) thru -1 of ("000000" & counter)
set newName to theName & divider & counter & theExtension
end repeat
end tell
return newName
end getUniqueName