Created
September 3, 2016 17:04
-
-
Save humitos/bd87066e3ded2e15e707583d9e93103f 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
# -*- coding: utf-8 -*- | |
# AWFUL synchronic version of NoSpotifyAds: the main idea is to detect silent | |
# into the stream and chunk it between of those silent. Then, if the chunk is | |
# between 25 and 35 seconds, instead of playing the ads we play a silent. | |
# OBVIOUS: it doesn't work! :) | |
# WHY?: it needs to be asynchronic. While we are playing, we need to detect | |
# silent and prepare the next chunk to be played. | |
# Once we start playing a song in Spotify, we need to wait at least a window of | |
# 40 seconds so 'noads.py' can start detecting the silents before start playing | |
# the current song. | |
from __future__ import division, print_function, unicode_literals | |
import pydub # fades | |
import pydub.playback | |
import pydub.silence | |
SILENCE_THRESHOLD = -32 | |
MIN_SILENCE_LENGHT = 3 * 1000 # 3 seconds | |
SEGMENENT_STREAM_LENGHT = 40 * 1000 # 40 seconds | |
MIN_SPOTIFY_ADS_THRESHOLD_LENGHT = 1 * 1000 # 1 seconds | |
MAX_SPOTIFY_ADS_THRESHOLD_LENGHT = 40 * 1000 # 40 seconds | |
FILEPATH = '/home/humitos/spotify-stream.ogg' # example | |
SILENT_STREAM = pydub.AudioSegment.silent(duration=SEGMENENT_STREAM_LENGHT) # to be played while ads | |
if __name__ == '__main__': | |
print('Loading file...') | |
stream = pydub.AudioSegment.from_ogg(FILEPATH) | |
sample_nro = 0 | |
sample_length = len(stream) | |
while sample_length >= SEGMENENT_STREAM_LENGHT: | |
print('Spliting on silence...') | |
segments = pydub.silence.split_on_silence( | |
stream[sample_nro * SEGMENENT_STREAM_LENGHT:(sample_nro + 1) * SEGMENENT_STREAM_LENGHT], | |
min_silence_len=3000, silence_thresh=-32) | |
for s in segments: | |
if MIN_SPOTIFY_ADS_THRESHOLD_LENGHT < len(s) < MAX_SPOTIFY_ADS_THRESHOLD_LENGHT: | |
print('Playing silence...') | |
pydub.playback.play(SILENT_STREAM) | |
else: | |
print('Playing music...') | |
pydub.playback.play(s) | |
sample_nro += 1 | |
sample_length -= SEGMENENT_STREAM_LENGHT | |
print('Quit.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment