Created
April 17, 2012 08:24
-
-
Save zonble/2404515 to your computer and use it in GitHub Desktop.
把音檔丟去給 QuickTime 問問看能不能播,PyObjC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # encoding: utf-8 | |
| import sys | |
| import getopt | |
| import os.path | |
| import objc, Cocoa, QTKit | |
| help_message = '''Input the path of the file that you want to validate.''' | |
| class Usage(Exception): | |
| def __init__(self, msg): self.msg = msg | |
| def validateAudioFile(fullpath): | |
| if not os.path.exists(fullpath): | |
| print "File does not exist." | |
| return False | |
| fileURL = Cocoa.NSURL.fileURLWithPath_(fullpath) | |
| print fileURL | |
| movie, error = QTKit.QTMovie.movieWithURL_error_(fileURL, None) | |
| if error: print error; return False | |
| if movie == None: return False | |
| attributes = movie.movieAttributes() | |
| if attributes == None: return False | |
| return True | |
| def main(argv=None): | |
| if argv is None: | |
| argv = sys.argv | |
| try: | |
| try: | |
| opts, args = getopt.getopt(argv[1:], "h", ["help"]) | |
| except getopt.error, msg: | |
| raise Usage(msg) | |
| # option processing | |
| for option, value in opts: | |
| if option in ("-h", "--help"): | |
| raise Usage(help_message) | |
| if not len(args): | |
| raise Usage(help_message) | |
| path = os.path.abspath(args[0]) | |
| playable = validateAudioFile(path) | |
| if playable: print "The file is playable." | |
| else: print "Not able to play the file." | |
| except Usage, err: | |
| print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg) | |
| print >> sys.stderr, "\t for help use --help" | |
| return 2 | |
| if __name__ == "__main__": | |
| sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment