Last active
October 6, 2017 16:33
-
-
Save fthiery/d37479b4e5413a70e731b02090b00976 to your computer and use it in GitHub Desktop.
This file contains 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 python3 | |
# -*- coding: utf-8 -*- | |
import gi | |
gi.require_version("Gst", "1.0") | |
gi.require_version("GstAudio", "1.0") | |
from gi.repository import Gst, GstAudio, GObject | |
import sys | |
GObject.threads_init() | |
Gst.init(None) | |
Gst.debug_set_default_threshold(Gst.DebugLevel.WARNING) | |
#Gst.debug_set_default_threshold(Gst.DebugLevel.DEBUG) | |
#Gst.debug_set_threshold_for_name("fdk*", 5) | |
#Gst.debug_set_threshold_for_name("qt*", 5) | |
import os | |
if not os.path.isdir('/tmp/hlstest'): | |
os.mkdir('/tmp/hlstest') | |
vsource_template = 'videotestsrc pattern=black num-buffers={numvbuffers} ! x264enc tune=zerolatency speed-preset=ultrafast key-int-max=30 ! h264parse ! queue ! mpegtsmux name=mux ! queue ! hlssink location=/tmp/hlstest/video%02d.ts playlist-location=/tmp/hlstest/playlist_{channel_count}.m3u8 target-duration=1' | |
asource_template = 'audiotestsrc freq={frequency} samplesperbuffer={samplingrate} num-buffers={numabuffers} ! audio/x-raw, format=(string)S16LE, layout=(string)interleaved, rate=(int){samplingrate}, channels=(int)1 ! queue ! adder.' | |
pipeline_template = 'audiointerleave name=adder ! fdkaacenc ! queue ! mux.' | |
# see gstfdkaacenc.c | |
GST_AUDIO_CHANNELS = { | |
1: [ | |
GstAudio.AudioChannelPosition.MONO | |
], | |
2: [ | |
GstAudio.AudioChannelPosition.FRONT_LEFT, | |
GstAudio.AudioChannelPosition.FRONT_RIGHT, | |
], | |
3: [ | |
GstAudio.AudioChannelPosition.FRONT_CENTER, | |
GstAudio.AudioChannelPosition.FRONT_LEFT, | |
GstAudio.AudioChannelPosition.FRONT_RIGHT, | |
], | |
4: [ | |
GstAudio.AudioChannelPosition.FRONT_CENTER, | |
GstAudio.AudioChannelPosition.FRONT_LEFT, | |
GstAudio.AudioChannelPosition.FRONT_RIGHT, | |
GstAudio.AudioChannelPosition.REAR_CENTER, | |
], | |
5: [ | |
GstAudio.AudioChannelPosition.FRONT_CENTER, | |
GstAudio.AudioChannelPosition.FRONT_LEFT, | |
GstAudio.AudioChannelPosition.FRONT_RIGHT, | |
GstAudio.AudioChannelPosition.SIDE_LEFT, | |
GstAudio.AudioChannelPosition.SIDE_RIGHT, | |
], | |
6: [ | |
GstAudio.AudioChannelPosition.FRONT_CENTER, | |
GstAudio.AudioChannelPosition.FRONT_LEFT, | |
GstAudio.AudioChannelPosition.FRONT_RIGHT, | |
GstAudio.AudioChannelPosition.SIDE_LEFT, | |
GstAudio.AudioChannelPosition.SIDE_RIGHT, | |
GstAudio.AudioChannelPosition.LFE1, | |
], | |
8: [ | |
GstAudio.AudioChannelPosition.FRONT_CENTER, | |
GstAudio.AudioChannelPosition.FRONT_LEFT, | |
GstAudio.AudioChannelPosition.FRONT_RIGHT, | |
GstAudio.AudioChannelPosition.SIDE_LEFT, | |
GstAudio.AudioChannelPosition.SIDE_RIGHT, | |
GstAudio.AudioChannelPosition.REAR_LEFT, | |
GstAudio.AudioChannelPosition.REAR_RIGHT, | |
GstAudio.AudioChannelPosition.LFE1, | |
] | |
} | |
try: | |
channelcount = int(sys.argv[1]) | |
except Exception: | |
print('Usage: %s 2' % sys.argv[0]) | |
sys.exit() | |
if not GST_AUDIO_CHANNELS.get(channelcount, False): | |
sys.exit('%s channels not supported' % channelcount) | |
duration_seconds = 10 | |
p = { | |
"channel_count": channelcount, | |
"frequency": 440, | |
"numvbuffers": duration_seconds * 30, | |
"numabuffers": duration_seconds, | |
"samplesperbuffer": 44100, | |
"samplingrate": 44100, | |
} | |
pipeline = pipeline_template | |
vsource = vsource_template.format(**p) | |
pipeline = " ".join((pipeline, vsource)) | |
for j in range(1, channelcount + 1): | |
p['frequency'] = 440 * j | |
asource = asource_template.format(**p) | |
pipeline = " ".join((pipeline, asource)) | |
pipeline = pipeline.format(**p) | |
print('Generating %s channel sample' % channelcount) | |
print(pipeline) | |
p = Gst.parse_launch(pipeline) | |
inter = p.get_by_name('adder') | |
pos = GST_AUDIO_CHANNELS[channelcount] | |
print('Setting positions: %s' % pos) | |
inter.set_property('channel-positions', pos) | |
def start(): | |
p.set_state(Gst.State.PLAYING) | |
bus = p.get_bus() | |
bus.add_signal_watch() | |
def on_eos(bus, message): | |
print('Got EOS, exiting') | |
sys.exit() | |
bus.connect("message::eos", on_eos) | |
ml = GObject.MainLoop() | |
GObject.idle_add(start) | |
ml.run() | |
# example: | |
# python generate_aac_samples.py 3 | |
# audiointerleave name=adder ! fdkaacenc ! queue ! mux. videotestsrc pattern=black num-buffers=5 ! x264enc tune=zerolatency speed-preset=ultrafast key-int-max=30 ! h264parse ! queue ! mpegtsmux name=mux ! queue ! hlssink location=/tmp/hlstest/video%02.ts playlist-location=/tmp/hlstest/playlist_3.m3u8 target-duration=1 audiotestsrc freq=440 samplesperbuffer=4410 num-buffers=50 ! audio/x-raw, format=(string)S16LE, layout=(string)interleaved, rate=(int)44100, channels=(int)1 ! queue ! adder. audiotestsrc freq=880 samplesperbuffer=4410 num-buffers=50 ! audio/x-raw, format=(string)S16LE, layout=(string)interleaved, rate=(int)44100, channels=(int)1 ! queue ! adder. audiotestsrc freq=1320 samplesperbuffer=4410 num-buffers=50 ! audio/x-raw, format=(string)S16LE, layout=(string)interleaved, rate=(int)44100, channels=(int)1 ! queue ! adder. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment