22
22
This module contains operations to convert sound files to WAV and to
23
23
retrieve a their metadata.
25
if __name__ == '__main__':
37
GVFS_SRC = "gnomevfssrc"
40
class ElementNotFoundError(KeyError):
41
"""This error is thrown when an element is not found"""
43
# Versions below 0.9 do not raise an exception when the element is not found
44
if gst.gst_version < (0, 9):
45
def safe_element_factory_make(*args, **kwargs):
46
element = gst.element_factory_make(*args, **kwargs)
48
raise ElementNotFoundError(args)
50
# New versions >= 0.9 already raise an exception, so there's not a big problem
52
safe_element_factory_make = gst.element_factory_make
31
54
class GstPlayingFailledError(StandardError):
32
55
"""This error is thrown when we can't set the state to PLAYING"""
249
272
NEW_PAD_SIGNAL = "new-pad"
250
273
GstOperation = Gst08Operation
275
def create_source(source, location, src_prop="location"):
276
src = safe_element_factory_make(source)
277
src.set_property(src_prop, location)
280
################################################################################
252
281
class AudioMetadataListener (operations.OperationListener):
254
283
The on_metadata event is called before the FinishedEvent, if the metadata
334
373
self.__metadata["duration"] = \
335
374
self.__oper.query_duration (gst.FORMAT_TIME)
337
def fileAudioMetadata (filename):
339
Returns the audio metadata from a file.
341
filesrc = gst.element_factory_make ("filesrc", "source")
342
filesrc.set_property ("location", filename)
343
return AudioMetadata(filesrc)
345
def gvfsAudioMetadata (uri):
347
Returns the audio metadata from an URI.
349
filesrc = gst.element_factory_make ("gnomevfssrc", "source")
350
filesrc.set_property ("location", uri)
351
return AudioMetadata(filesrc)
376
def get_metadata(source, location):
377
return AudioMetadata(create_source(source, location))
354
380
################################################################################
361
387
'endianness': 1234
364
WavPcmParse = ("audio/x-raw-int, endianness=(int)1234, width=(int)16, "
390
_WAV_PCM_PARSE = ("audio/x-raw-int, endianness=(int)1234, width=(int)16, "
365
391
"depth=(int)16, signed=(boolean)true, rate=(int)44100, "
366
392
"channels=(int)2")
368
def capsIsWavPcm (caps):
394
def is_caps_wav_pcm (caps):
372
396
if not struct.get_name () == "audio/x-raw-int":
407
433
sink.set_property ("signal-handoffs", True)
408
434
sink.connect ("handoff", self.on_handoff)
410
waveparse = bin.get_by_name ("iwp_wavparse")
411
waveparse.connect (NEW_PAD_SIGNAL, self.on_new_pad)
436
self.waveparse = bin.get_by_name ("iwp_wavparse")
437
self.waveparse.connect (NEW_PAD_SIGNAL, self.on_new_pad)
413
439
self.oper.bin.add (source)
414
440
source.link (decoder)
442
self.is_wav_pcm = False
418
444
def on_handoff (self, *args):
419
445
self.oper.stop ()
421
447
def on_new_pad (self, src, pad):
422
448
caps = pad.get_caps()
423
self.isWavPcm = capsIsWavPcm (caps)
449
self.is_wav_pcm = is_caps_wav_pcm (caps)
425
451
def on_finished (self, event):
426
if event.id != operations.ERROR and self.isWavPcm:
427
assert event.id == operations.SUCCESSFUL or event.id == operations.ABORTED
452
if event.id != operations.ERROR and self.is_wav_pcm:
453
assert event.id == operations.SUCCESSFUL or \
454
event.id == operations.ABORTED
429
456
self._send_finished_event (operations.SUCCESSFUL)
449
476
running = property (lambda self: self.__oper != None)
452
def fileIsPcmWav (filename):
453
src = gst.element_factory_make ("filesrc")
454
src.set_property ("location", filename)
455
return IsWavPcm (src)
456
fileIsPcmWav = operations.async(fileIsPcmWav)
457
fileIsPcmWav = operations.operation_factory(fileIsPcmWav)
459
def gvfsIsPcmWav(uri):
460
src = gst.element_factory_make("gnomevfssrc")
461
src.set_property("location", uri)
464
gvfsIsPcmWav = operations.async(gvfsIsPcmWav)
465
gvfsIsPcmWav = operations.operation_factory(gvfsIsPcmWav)
467
def sourceToWav (source, sink):
478
def is_wav_pcm(source, location):
479
return IsWavPcm(create_source(source, location))
481
is_wav_pcm = operations.operation_factory(is_wav_pcm)
482
is_wav_pcm = operations.async(is_wav_pcm)
484
################################################################################
485
def source_to_wav(source, sink):
469
487
Converts a given source element to wav format and sends it to sink element.
471
489
To convert a media file to a wav using gst-launch:
472
source ! decodebin ! audioconvert ! audioscale !$WavPcmParse ! wavenc
490
source ! decodebin ! audioconvert ! audioscale !$_WAV_PCM_PARSE ! wavenc
474
493
bin = gst.parse_launch (
475
494
"decodebin name=stw_decodebin !"
476
495
"audioconvert ! "
478
497
" ! wavenc name=stw_wavenc"
480
499
oper = GstOperation(sink, bin)
492
sourceToWav = operations.operation_factory(sourceToWav)
495
def fileToWav (src_filename, sink_filename):
511
source_to_wav = operations.operation_factory(source_to_wav)
513
def convert_to_wav(source, source_location, sink_location):
497
515
Utility function that given a source filename it converts it to a wav
498
516
with sink_filename.
500
src = gst.element_factory_make ("filesrc")
501
src.set_property ("location", src_filename)
502
sink = gst.element_factory_make ("filesink")
503
sink.set_property ("location", sink_filename)
504
return sourceToWav (src, sink)
518
sink = safe_element_factory_make("filesink")
519
sink.set_property("location", sink_location)
520
return source_to_wav(create_source(source, source_location), sink)
522
convert_to_wav = operations.operation_factory(convert_to_wav)
523
convert_to_wav = operations.async(convert_to_wav)
526
"convert": convert_to_wav,
527
"is_wav": is_wav_pcm,
528
"get_metadata": get_metadata
531
def parse_command(operation, source, source_location, *args):
532
return commands[operation](source, source_location, *args)
506
534
################################################################################
508
536
################################################################################
509
537
if __name__ == '__main__':
512
541
mainloop = gobject.MainLoop ()
513
class L (GstOperationListener):
514
def __init__(self, foo):
542
class Listener (GstOperationListener):
543
def __init__(self, oper):
517
546
def on_metadata (self, event, metadata):
547
print >> sys.stderr, metadata
520
549
def on_finished (self, event):
521
if event.id == operations.ABORTED:
524
if event.id == operations.ERROR:
525
print "Error:", event.error
550
self.success = operations.SUCCESSFUL == event.id
529
553
def on_progress (self):
554
print self.oper.progress
534
#f = fileToWav (sys.argv[1], sys.argv[2])
535
f = gvfsIsPcmWav (sys.argv[1])
536
print operations.syncOperation (f).id == operations.SUCCESSFUL
538
#f = fileAudioMetadata (sys.argv[1])
557
#f = convert_to_wav (FILE_SRC, sys.argv[1], sys.argv[2])
558
f = parse_command(sys.argv[1], FILE_SRC, sys.argv[2], *sys.argv[3:])
559
#f = is_wav_pcm (FILE_SRC, sys.argv[1])
560
#print operations.syncOperation (f).id == operations.SUCCESSFUL
562
#f = get_metadata("filesrc", sys.argv[1])
539
563
#f = extractAudioTrackFile ("/dev/cdrom", int(sys.argv[1]) + 1, sys.argv[2])
541
#gobject.timeout_add (200, l.on_progress)
565
if isinstance(f, operations.MeasurableOperation):
566
gobject.timeout_add (200, l.on_progress)
542
567
f.listeners.append (l)
544
569
l.finished = False
548
# while not l.finished: