Scripting Help

Discussion in 'Mac Programming' started by ninethirty, Feb 13, 2011.

  1. ninethirty macrumors 6502a

    Joined:
    Mar 1, 2006
    #1
    Hey Everyone,

    First of all... I'm not a programmer in any way at all. I can do some basic command line stuff, but for the most part, I don't have a clue what I'm doing :)

    I've recently gotten an AppleTV (which I love) and I've been converting all my old MKV files to M4V using command line ffmpeg.

    Recently I got to thinking... wouldn't it just be easy if I had a folder that as soon as an MKV was dropped in would do the following:

    • Run command line ffmpeg (ffmpeg -i FILE.mkv -vcodec copy -acodec copy FILE.m4v)
    • Delete the MKV
    • Place the M4V in iTunes
    • Delete the original M4V

    I know you're probably all laughing at how simple this is, but if someone could help me figure out what I need to do to accomplish this.. you'd be making someone very happy :)

    Cheers in advance, and apologies if this is the wrong place for this.
     
  2. subsonix, Feb 13, 2011
    Last edited: Feb 13, 2011

    subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    What you want to do is look into Automator, and folder actions. Basically anything dropped inside such a folder will get the action applied to it.

    Edit: You can also make it a "service" from Automator which will add it to the context menu and show up whenever you right click on an object which makes sense to the script. (this applies to both methods) Inside Automator choose the action run shell script, then selected items as argument. Automator will generate an empty for loop inside it, then you just add your script. All you need to do is replacing the filenames with the argument variables.
     
  3. ninethirty thread starter macrumors 6502a

    Joined:
    Mar 1, 2006
    #3
    Thanks for your help!

    I got as far as the run shell script action. No clue what to do after that :)

    I'm guessing that I've waded a bit too far into something I know nothing about here. I'll try to figure it out on my own, but if you have the time and you're able to be more specific, I'd really appreciate it.

    Cheers!

     
  4. subsonix, Feb 13, 2011
    Last edited: Feb 13, 2011

    subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    You get some drop down menus at the top, you would probably use "movie files" in "finder". Inside the run shell scrip box you have an automated loop that should look like this:

    Code:
    for f in "$@"
    do
    	ffmpeg -i "$f" -vcodec copy -acodec copy "${f%.*}.m4v"
    done
    
    The only thing you need to do is to change the file name to "$f" then add an extension to the output file. Can you post the normal ffmpeg command line you normally use?


    Also, macbreak have some nice podcasts on using Automator that you should check out, available in iTunes.

    Edit: Sorry I missed that you actually posted the line above, edited post.

    Try this on one some unimportant file as this is untested, ha ha.
     
  5. ninethirty thread starter macrumors 6502a

    Joined:
    Mar 1, 2006
    #5
    It's as I wrote above:

    ffmpeg -i input.mkv -vcodec copy acodec copy outpout.mkv

    Sometimes, it's more complicated than that, but usually that will do.

    Cheers!

     
  6. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #6
    I saw that and edited the post. If you save this as a service it should show up in your context menu. Keep in mind to test this, I haven't. ;)
     
  7. ninethirty thread starter macrumors 6502a

    Joined:
    Mar 1, 2006
    #7
    Thanks! I added the script code, but it tells me that the shell script failed, and says ffmpeg is not a recognized command.

    Weird, because it works from anywhere in the terminal..

     
  8. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #8
    Strange, try to add the full path to ffmpeg and see if that works.

    An alternative to this is to look for actions for ffmpeg, a google for ffmpeg automator action revealed that some where available. Never tried any of them though. But as far as importing to iTunes and deleting/renaming files and so on. These are available as separate actions in Automator in the left pane. Automator simply chain these together to create an automated chain of actions.
     
  9. ninethirty, Feb 13, 2011
    Last edited: Feb 13, 2011

    ninethirty thread starter macrumors 6502a

    Joined:
    Mar 1, 2006
    #9
    First of all, thank you for being so patient with me!

    I used this code earlier today in terminal itself (not an action)

    for f in *.mkv ;
    do FILE=${f%%.mkv};
    ffmpeg -i "$FILE.mkv" -vcodec copy -acodec copy "$FILE.m4v"
    done

    that worked in the terminal, but when I try that in automator, it fails and gives the error.

    When I use the code that you gave me, the script completes, but it doesn't actually run ffmpeg on the sample MKV I have in the folder at the moment.

    Any idea why in the code I have, it doesn't locate ffmpeg?

     
  10. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #10
    I don't know, did you remember to change the input drop down from "stdin" to as arguments? If not, then your selected files wont be redirected to the script. Beyond that I don't know, but as I said, supplying the full path to ffmpeg should resolve the problem of locating it.

    I have used this method myself to batch convert files, just never with ffmpeg, which I don't even have installed so I can't test it either.
     
  11. ninethirty thread starter macrumors 6502a

    Joined:
    Mar 1, 2006
    #11
    I used your code, and the full path and that worked! Thank you very much.

    One last question. Is there something I can put in the script that will delete the original MKV file after the conversion has been made?

    Also, will I confuse the folder if I drop multiple .MKV's in at once?

    Cheers!

     
  12. subsonix, Feb 13, 2011
    Last edited: Feb 13, 2011

    subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #12
    After the ffmpeg line adding this:

    Code:
    rm "$f"
    on the line below should do it. But, test it on some garbage files first.

    Not that I can see, the for loop should take care of all files given. But, test it and see. :)


    Important consideration:

    There might be some side effects that you should consider perhaps. What if ffmpeg fails to convert the file for some reason, perhaps due to format or something else? Then the original could get deleted, without leaving a converted copy for you.

    A safer approach is probably to first check for the new file, if present delete the old. Or just move the old the the trash, then you can get it back at least.

    Code:
    mv "$f" ~/.Trash/
     
  13. ninethirty thread starter macrumors 6502a

    Joined:
    Mar 1, 2006
    #13
    You're awesome. Thanks so much for all your help!
     

Share This Page