# MoPiD 1.0b by Joey C. (http://joeyjwc.x3fusion.com/) - Username joeyjwc on the iPL forums.
# Copyright 2005 Joey C.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os, sys, win32api, win32ui
isize = {"nano": "scale=176:-2,expand=176:132,format=bgr16", "photo": "scale=220:-2,expand=220:176,format=bgr16", "reg": "scale=160:-2,expand=160:128,format=bgr24", "mini": "scale=138:-2,expand=138:110,format=bgr24" }
sysdir = os.getcwd()
fornot = ""
tbppconvparams = ""
fps = ""
vernum = "1.21b"
theos = "win"

def mencodeit(iptype, outpfile):
    global fornot, fps
    thecommand = win32api.GetShortPathName(sysdir + "\\mencoder\\mencoder.exe") + fornot + " -ovc raw -ofps " + fps + " -oac pcm -vf " + iptype + " \"" + inpfile + "\" -o \"" + outpfile + "\""
    print thecommand
    os.system(thecommand)

def twobppit(outpufile):
    thecommand = win32api.GetShortPathName(sysdir + "\\2bppconv\\2bppconv.exe") + " " + tbppconvparams + " \"tmp.avi\" \"" + outpufile + "\""
    print thecommand
    os.system(thecommand)
    os.remove("tmp.avi")

def getinfo():
    global fornot, ipodtype, inpfile, outfile
    ipodtype = raw_input("iPod Type (reg, mini, photo, nano): ")
    i = win32ui.CreateFileDialog(1, "avi", None, 0, None, None)     #The method of using the built-in Windows UI to set up a file dialog.
    i.SetOFNTitle("Select an AVI File to Open")                     #When I eventually port this to Linux, I need to remove these lines.
    i.DoModal()                                                     #Perhaps I could use Tkinter as a replacement.
    inpfile = i.GetPathName()                                       #
    o = win32ui.CreateFileDialog(0, "avi", None, 0, None, None)     #
    o.SetOFNTitle("Please Save your AVI File")                      #
    o.DoModal()                                                     #
    outfile = o.GetPathName()   	                            #
    if i.GetFileExt().lower() != "avi":                             #
        print "This file will be using the -of command."            #
        fornot = " -of avi"                                         #
    if outfile != os.path.join(os.getcwd(), "tmp.avi") and inpfile != outfile:
        checkvals()
    else:
        print "ERROR: One or more of the filenames are invalid; please do not use tmp.avi and please do not let the input filename be the same as the output filename."
        getinfo()

def get2bppparams():
    global tbppconvparams
    cstyle = raw_input("Dithering Type (reg*, 256shade, none): ")
    if cstyle == "256shade":
        tbppconvparams = tbppconvparams + " -r"
    elif cstyle == "none":
        tbppconvparams = tbppconvparams + " -d"

def getfps():
    global fps
    fpval = raw_input("FPS (15*, 12): ")
    if fpval == "12":
        fps = "12"
    else:
        fps = "15"
        

def checkvals():
    if not os.path.exists(inpfile):
        print "ERROR: Input File Doesn't Exist."
        getinfo()
    elif os.path.exists(outfile):
        print "WARNING: File Exists!"
        checkifokay()
    else:
        findipodtype()

def checkifokay():        
    isokay = raw_input("Okay to Overwrite? (y, n): ")
    if isokay == "n":
        getinfo()
    elif isokay == "y":
        os.remove(outfile)
        findipodtype()
    else:
        checkifokay()

def findipodtype():
    global tbppconvparams
    getfps()
    if ipodtype == "photo" or ipodtype == "nano":
        mencodeit(isize[ipodtype], outfile)
    elif ipodtype == "reg" or ipodtype == "mini":
        if ipodtype == "mini":
            print "Using the -k option to fix straight lines on Mini."
            tbppconvparams = tbppconvparams + " -k"
        get2bppparams()
        mencodeit(isize[ipodtype], "tmp.avi")
        twobppit(outfile)
    else:
        print "ERROR: Input Invalid."
        getinfo()

print "MoPiD - The Easy Video Converter for iPodLinux"
print "By Joey C. (http://joeyjwc.x3fusion.com/)"
print "Information"
print "-----------"
print "Version: " + vernum
print "Current Directory: " + sysdir
print "OS: " + theos
print "\n---"
print "An asterisk indicates the default value."
print "Hit CTRL+C to exit."
print "---\n"

getinfo()
raw_input("\nFinished.  Press enter to exit.")
