config.py added
[tsr.git] / convert.py
blob2bb8eac504ac34fe1282a05958d6b018bfa6cd6a
1 #!/usr/bin/env python
2 """
3 Convertion part for TSR converts the supplyed flv's to vairus formats
4 using ffmpeg.
6 """
8 # Copyright 2010 Bug
9 # This file is part of TSR (The stream ripper).
11 # TSR is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation, either version 3 of the License, or
14 # (at your option) any later version.
16 # TSR is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with TSR. If not, see <http://www.gnu.org/licenses/>.
24 import os, sys, ConfigParser, subprocess, getopt
25 HOME = os.getenv('USERPROFILE') or os.getenv('HOME')
26 if os.path.isfile(HOME + '/.tsr.cfg'):
27 CONFIGFILE = 'tsr.cfg'
28 elif os.path.isfile('tsr.cfg'):
29 CONFIGFILE = 'tsr.cfg'
30 else:
31 CONFIGFILE = None
33 VERSION = '0.0.001'
35 def readconfigvideo():
36 """Reads the config and returns the video format to be used"""
37 config = ConfigParser.SafeConfigParser({'video': 'avi'})
38 if CONFIGFILE:
39 config.read(CONFIGFILE)
40 return config.get('Convert', 'video')
41 else:
42 return 'avi'
44 def readconfigaudio():
45 """Reads the config and returns the audio format to be used"""
46 config = ConfigParser.SafeConfigParser({'video': 'oga'})
47 if CONFIGFILE:
48 config.read(CONFIGFILE)
49 return config.get('Convert', 'audio')
50 else:
51 return 'oga'
53 def readconfigffmpeg():
54 """Reads the config and returns ffmpeg path"""
55 config = ConfigParser.SafeConfigParser({'video': 'ffmpeg'})
56 if CONFIGFILE:
57 config.read(CONFIGFILE)
58 return config.get('Convert', 'ffmpeg')
59 else:
60 return 'ffmpeg'
62 def getfilename(filepath):
63 """Returns the name of the file without the extention"""
64 if os.path.isfile(filepath):
65 name = os.path.split(filepath)
66 name = os.path.splitext(name[1])
67 return name[0]
68 else:
69 return None
71 def convert(name, targetformat=None):
72 """Converts given file to a given format and returns new file name"""
73 if targetformat is None:
74 targetformat = readconfigvideo()
75 #Still gotta fix the ffmpeg call...
76 #Also gotta make it non depanded no ffmpeg and capable of using
77 #other convertors...
78 #And figure a better way to control the command.
79 command = [readconfigffmpeg(), '-i', name]
80 # Specail switches per format
81 if targetformat == 'oga':
82 command.append('-vn')
83 command.append('-f')
84 command.append('ogg')
85 newname = getfilename(name) + '.' + targetformat
86 command.append(newname)
87 subprocess.Popen(command, stderr=subprocess.STDOUT, \
88 stdout=subprocess.PIPE).communicate()[0]
89 return newname
91 def help_message():
92 """Print an help message for the program and quits"""
93 print '''
94 {0} converts a file to another format using ffmpeg
96 options:
97 -h or --help Display this help message
98 -e or --encoding extention
99 Forces the convertor to convert to that format
100 -a or --audio-only Convert to audio only
101 -v or --video Convert both audio and video
102 --encoding, --audio and --video should not be used at the same
103 time
104 --version Display version information
106 This is part of The Stream Ripper.'''.format(sys.argv[0])
107 sys.exit(0)
108 def version_message():
109 """Prints version information"""
110 print """
111 Version {0}""".format(VERSION)
112 sys.exit(0)
114 def main():
115 """Self debug and other stuff?"""
118 try:
119 options, xarguments = getopt.getopt(sys.argv[1:], 'he:av', \
120 ['help', 'encoding:', 'audio-only', 'video', 'version'])
121 except getopt.error:
122 print 'Error: You tried to use an unknown option or the'
123 print 'argument for an option that requires it was missing.'
124 print 'Try ' + sys.argv[0] + ' -h for more information.'
125 sys.exit(0)
127 # Assumptions so code won't die
128 target = None
130 for a in options[:]:
131 if a[0] == '-h' or a[0] == '--help':
132 help_message()
133 for a in options[:]:
134 if a[0] == '--version':
135 version_message()
136 for a in options[:]:
137 if a[0] == '-e' or a[0] == '--encoding':
138 target = a[1]
139 options.remove(a)
140 break
141 for a in options[:]:
142 if a[0] == '-a' or a[0] == 'audio':
143 target = readconfigaudio()
144 options.remove(a)
145 break
146 for a in options[:]:
147 if a[0] == '-v' or a[0] == 'video':
148 target = readconfigvideo()
149 options.remove(a)
150 break
151 if not xarguments:
152 print 'Syntax: {0} [-e encoding / -a / -v] file'.format( \
153 sys.argv[0])
154 sys.exit()
155 for name in xarguments:
156 convert(name, target)
158 return 0
160 if __name__ == '__main__':
161 main()