Question

I'm trying to get an applescript to combine .mkv's with .srt's after i added those files to a folder.

I've managed to do the automatic conversion from mkv to mp4 and add them to itunes. The only thing that I cannot accomplish is to get the srt automatically.

I also can do this in the Terminal window, one "mkv" at a time. The command that is working fine for me to do this, is:

ffmpeg -i movie.1.mkv -sub_charenc WINDOWS-1252 -i movie.1.srt -vcodec copy -acodec copy -scodec mov_text -metadata:s:s:0 language=por movie.1.mp4

I have the mkv file with the same name as the srt file.

The apple script I'm using to do the batch conversion is:

on adding folder items to this_folder after receiving added_items
tell application "Finder"
    set fold_name to the name of this_folder
    try

        repeat with i from 1 to number of items in added_items
            set new_item to item i of added_items
            set the item_path to the quoted form of the POSIX path of new_item

            if name extension of new_item is "mkv" then
                do shell script " /usr/local/bin/ffmpeg -i " & item_path & " -vcodec copy -acodec libfaac " & item_path & ".mp4"

            else if name extension of new_item is "mp4" then
                tell application "iDentify"
                    activate
                    open new_item
                end tell

            end if
        end repeat
    end try
end tell end adding folder items to

I'm a newbie in the applescript, and the thing why i don't get it to work is, that the script returns in the end a file called movie.1.mkv.mp4 created from the movie.1.mkv. That means that I'm not abble to get in the variable "item_path" only the filename without the extension. Perhaps it's very easy, but i didn't manage to do it.

Can anyone please help? Thank you

Was it helpful?

Solution

Using and adapting code from

https://stackoverflow.com/questions/12907517/extracting-file-extensions-from-applescript-paths

Add to the top of your code:

-- overcome finder limitation...
set text item delimiters to "."

then later extract the name and extension portions...

if new_item contains "." then set item_ext to (text item 2 of new_item) as text
if new_item contains "." then set item_name to (text items 1 thru -2 of new_item) as text

That should do it... :)

Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top