Skip to content

Instantly share code, notes, and snippets.

@kn0ll
Created September 12, 2015 07:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kn0ll/ae53a1f216f7bb728ef7 to your computer and use it in GitHub Desktop.
Save kn0ll/ae53a1f216f7bb728ef7 to your computer and use it in GitHub Desktop.
super hacky script for taking a song off soundcloud and creating a new version where the computer incessantly reads user comments over the audio
import random
import urllib
import urllib2
import soundcloud
import subprocess
from pydub import AudioSegment
def get_track(client_id, sc_url):
client = soundcloud.Client(client_id=client_id)
track = client.get('/resolve', url=sc_url)
stream = client.get(track.stream_url, allow_redirects=False)
mp3 = (urllib2.urlopen(stream.location)).read()
comments = client.get('/tracks/%s/comments' % (track.id))
return {
'track': track,
'mp3': mp3,
'comments': comments
}
def write_file(data, location):
f = open(location, 'wb')
f.write(data)
def get_random_comment(comments):
return random.choice(comments).body
def get_random_comments(comments, count):
return [comments[i] for i in sorted(random.sample(xrange(len(comments)), count))]
track = get_track('your_soundcloud_client_id',
'https://soundcloud.com/aleksander-vinter/noob-brodinski-peanuts-club-blanco-remix')
# create song mp3
write_file(track['mp3'], 'tmp/song.mp3')
# get comments to speechify
comments = get_random_comments(track['comments'], 22)
# mix audio
song = AudioSegment.from_mp3('tmp/song.mp3')
combined = song
for comment in comments:
print 'writing comment', comment.body, comment.timestamp
subprocess.call(['say', '-o', 'tmp/speech.wav', '--data-format=LEF32@22050', comment.body])
speech = AudioSegment.from_wav('tmp/speech.wav')
combined = combined.overlay(speech, position=comment.timestamp)
# write combined wav
combined.export('tmp/combined.wav', format='wav')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment