1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
#
# OggConvert -- Converts media files to Free formats
# (c) 2007 Tristan Brindle <tcbrindle at gmail dot com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser 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 gobject
import pygst
pygst.require("0.10")
import gst
import ocv_constants
class Transcoder(gobject.GObject):
__gsignals__ = {
'paused' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, []),
'playing' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, []),
'eos' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, [])
}
def __init__(self, infile, outfile, vformat, vquality, aquality, fformat):
gobject.GObject.__init__(self)
self._infile = infile
self._outfile = outfile
self._vformat = vformat
if self._vformat == "THEORA":
self._vquality = ocv_constants.THEORA_QUALITY_MAPPING[vquality]
elif self._vformat == "SCHRO":
self._vquality = ocv_constants.SCHRO_QUALITY_MAPPING[vquality]
else:
# We should never get here
self._vquality = 0
self._aquality = ocv_constants.VORBIS_QUALITY_MAPPING[aquality]
self._fformat = fformat
self._pipeline = gst.Pipeline()
#self._filesrc = gst.element_factory_make("filesrc")
#self._filesrc.set_property("location", self._infile)
self._filesrc = gst.element_make_from_uri(gst.URI_SRC, infile)
self._decodebin = gst.element_factory_make("decodebin")
self._decodebin.connect("new-decoded-pad", self._on_new_pad)
if fformat == "OGG":
self._mux = gst.element_factory_make("oggmux")
for key in ocv_constants.OGGMUX_OPTS:
self._mux.set_property(key, ocv_constants.OGGMUX_OPTS[key])
else:
self._mux = gst.element_factory_make("matroskamux")
for key in ocv_constants.MATROSKAMUX_OPTS:
self._mux.set_property(key, ocv_constants.MATROSKAMUX_OPTS[key])
self._progreport = gst.element_factory_make("progressreport")
self._filesink = gst.element_factory_make("filesink")
self._filesink.set_property("location", outfile)
self._pipeline.add(self._filesrc,
self._decodebin,
self._mux,
self._progreport,
self._filesink)
# Link up what we've got so far
self._filesrc.link(self._decodebin)
self._mux.link(self._progreport)
self._progreport.link(self._filesink)
# Connect up callbacks
self.bus = self._pipeline.get_bus()
self.bus.add_signal_watch()
self.bus.connect("message::eos",self._on_end_of_stream)
self.bus.connect("message::state-changed", self._on_state_changed)
def play(self):
self._pipeline.set_state(gst.STATE_PLAYING)
def pause(self):
self._pipeline.set_state(gst.STATE_PAUSED)
def stop(self):
self.bus.remove_signal_watch()
self._pipeline.set_state(gst.STATE_NULL)
def sync(self):
"""
Wait for the pipeline to change state
We give it a 3 second timeout: returns True if the state change was
successful in this time, False otherwise
"""
state = self._pipeline.get_state(timeout=3*gst.SECOND)[0]
if state == gst.STATE_CHANGE_SUCCESS:
return True
else:
return False
def get_position(self):
try:
return self._pipeline.query_position(gst.FORMAT_TIME)[0]
except gst.QueryError:
return None
def get_duration(self):
try:
return self._pipeline.query_duration(gst.FORMAT_TIME)[0]
except gst.QueryError:
return None
def _on_new_pad(self, element, pad, args):
caps = pad.get_caps()
pad_type = caps.to_string()[0:5]
if pad_type == "video":
encoder = VideoEncoder(self._vformat, self._vquality)
self._pipeline.add(encoder)
encoder.set_state(gst.STATE_PAUSED)
pad.link(encoder.get_pad("sink"))
encoder.link(self._mux)
elif pad_type == "audio":
encoder = AudioEncoder(self._aquality)
self._pipeline.add(encoder)
encoder.set_state(gst.STATE_PAUSED)
pad.link(encoder.get_pad("sink"))
encoder.link(self._mux)
else:
print "Unknown pad type detected, %s" %pad_type
def _on_end_of_stream(self, bus, message):
self.emit('eos')
def _on_state_changed(self, bus, message):
if (message.src == self._pipeline):
state = message.parse_state_changed()
if state[1] == gst.STATE_PAUSED:
self.emit('paused')
elif state[1] == gst.STATE_PLAYING:
self.emit('playing')
class VideoEncoder(gst.Bin):
def __init__(self, format, quality):
gst.Bin.__init__(self)
self._queue1 = gst.element_factory_make("queue")
self._queue1.set_property("max-size-buffers",500)
self._queue2 = gst.element_factory_make("queue")
self._queue1.set_property("max-size-buffers",500)
self._ffmpegcsp = gst.element_factory_make("ffmpegcolorspace")
self._videorate = gst.element_factory_make("videorate")
if format == "SCHRO":
self.encoder = gst.element_factory_make("schroenc")
for prop in ocv_constants.SCHRO_OPTS:
self.encoder.set_property(prop, ocv_constants.SCHRO_OPTS[prop])
try:
self.encoder.set_property("noise-threshold", quality)
except TypeError:
print "This version of schroenc doesn't have the quality property"
else:
self.encoder = gst.element_factory_make("theoraenc")
for prop in ocv_constants.THEORA_OPTS:
self.encoder.set_property(prop, ocv_constants.THEORA_OPTS[prop])
self.encoder.set_property("quality", quality)
self.add(self._queue1,
self._ffmpegcsp,
self._videorate,
self.encoder,
self._queue2)
gst.element_link_many(self._queue1,
self._ffmpegcsp,
self._videorate,
self.encoder,
self._queue2)
# Create GhostPads
self.add_pad(gst.GhostPad('sink', self._queue1.get_pad('sink')))
self.add_pad(gst.GhostPad('src', self._queue2.get_pad('src')))
class AudioEncoder(gst.Bin):
def __init__(self, quality):
gst.Bin.__init__(self)
self._queue1 = gst.element_factory_make("queue")
self._queue1.set_property("max-size-buffers",500)
self._queue2 = gst.element_factory_make("queue")
self._queue2.set_property("max-size-buffers",500)
self._audioconvert = gst.element_factory_make("audioconvert")
# Vorbisenc can't handle packets without timestamps very well.
# This means that certain demuxers (asfdemux and matroskademux for
# example, but also most of the ffdemux elements) will generate
# very choppy audio.
#
# Fortunately GStreamer includes an audiorate element which can fix up
# such broken streams, by putting the right timestamps on and so forth.
# Unfortunately the audiorate element is broken in 0.10.10, to the extent
# that the pipeline won't even preroll. Therefore we check the GStreamer
# version (in __init__.py) to decide whether to use it or not. If you want
# to override this checking for any reason, you can do so by editing
# ocv_constants.py
if ocv_constants.USE_AUDIORATE:
self._audiorate = gst.element_factory_make("audiorate")
else:
self._audiorate = gst.element_factory_make("identity")
self.encoder = gst.element_factory_make("vorbisenc")
# Set Vorbis options
for prop in ocv_constants.VORBIS_OPTS:
self.encoder.set_property(prop, ocv_constants.VORBIS_OPTS[prop])
self.encoder.set_property("quality", quality)
self.add(self._queue1,
self._audioconvert,
self._audiorate,
self.encoder,
self._queue2)
gst.element_link_many(self._queue1,
self._audioconvert,
self._audiorate,
self.encoder,
self._queue2)
# Create GhostPads
self.add_pad(gst.GhostPad('sink', self._queue1.get_pad('sink')))
self.add_pad(gst.GhostPad('src', self._queue2.get_pad('src')))
|