3 Convertion part for TSR converts the supplyed flv's to vairus formats
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'
35 def readconfigvideo():
36 """Reads the config and returns the video format to be used"""
37 config
= ConfigParser
.SafeConfigParser({'video': 'avi'})
39 config
.read(CONFIGFILE
)
40 return config
.get('Convert', 'video')
44 def readconfigaudio():
45 """Reads the config and returns the audio format to be used"""
46 config
= ConfigParser
.SafeConfigParser({'video': 'oga'})
48 config
.read(CONFIGFILE
)
49 return config
.get('Convert', 'audio')
53 def readconfigffmpeg():
54 """Reads the config and returns ffmpeg path"""
55 config
= ConfigParser
.SafeConfigParser({'video': 'ffmpeg'})
57 config
.read(CONFIGFILE
)
58 return config
.get('Convert', '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])
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
78 #And figure a better way to control the command.
79 command
= [readconfigffmpeg(), '-i', name
]
80 # Specail switches per format
81 if targetformat
== 'oga':
85 newname
= getfilename(name
) + '.' + targetformat
86 command
.append(newname
)
87 subprocess
.Popen(command
, stderr
=subprocess
.STDOUT
, \
88 stdout
=subprocess
.PIPE
).communicate()[0]
92 """Print an help message for the program and quits"""
94 {0} converts a file to another format using ffmpeg
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
104 --version Display version information
106 This is part of The Stream Ripper.'''.format(sys
.argv
[0])
108 def version_message():
109 """Prints version information"""
111 Version {0}""".format(VERSION
)
115 """Self debug and other stuff?"""
119 options
, xarguments
= getopt
.getopt(sys
.argv
[1:], 'he:av', \
120 ['help', 'encoding:', 'audio-only', 'video', 'version'])
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.'
127 # Assumptions so code won't die
131 if a
[0] == '-h' or a
[0] == '--help':
134 if a
[0] == '--version':
137 if a
[0] == '-e' or a
[0] == '--encoding':
142 if a
[0] == '-a' or a
[0] == 'audio':
143 target
= readconfigaudio()
147 if a
[0] == '-v' or a
[0] == 'video':
148 target
= readconfigvideo()
152 print 'Syntax: {0} [-e encoding / -a / -v] file'.format( \
155 for name
in xarguments
:
156 convert(name
, target
)
160 if __name__
== '__main__':