Fun with Audiosurf 2 and Mitmproxy

I enjoy firing up Audiosurf 2 from time to time to have some rythmic musical fun. One of the things that has been bothering me, however, is the fact that Audiosurf 2 only integrates with Soundcloud for its music streaming. You can use local files, but since I stream all of my music from YouTube or Spotify, this always leaves me with a limited collection of songs to choose from, and I wondered whether there was a way to circumvent this.

Enter mitmproxy, a powerful interactive console program that allows network traffic to be intercepted, inspected, modified and replayed. mitmproxy has a powerful scripting API that allows you to modify traffic on the fly using simple Python scripts. Perhaps we could use this to make Audiosurf 2 think it is connected to Soundcloud whilst we actually query and download music from YouTube instead.

Let’s go through the script from top to bottom. First, we set up our imports and some constants…

import youtube_dl
import copy
import json
import requests
from time import sleep
from urlparse import urlparse, parse_qs
from urllib import quote
from mitmproxy.models import HTTPResponse
from netlib.http import Headers

    'format': 'bestaudio[ext=mp3]/bestaudio[ext=m4a]/bestaudio',
    'progress_hooks': [],
#   'postprocessors': [{
#       'key': 'FFmpegExtractAudio',
#       'preferredcodec': 'mp3',
#       'preferredquality': '192',
#   }],

You can enable a postprocessor in youtube_dl (the Python library we’ll be using to handle the actual downloads from YouTube) to convert any movie to an MP3 with FFmpeg, though this can take up a lot of time. The 'bestaudio[ext=mp3]/bestaudio[ext=m4a]/bestaudio' format setting works better.

Next up, we need to set up a YouTube API key and a fake response dict representing a track as it would come from the Soundcloud API:

YOUTUBE_API_KEY = '--- get a youtube api key and set this here ---'
        "last_modified":"2010/02/16 17:41:26 +0000"},
    "created_at":"2010/04/14 20:46:00 +0000",
    "last_modified":"2016/08/09 23:59:27 +0000",

Next, we set up the actual hook to intercept all requests to

def request(context, flow):
    YDL_OPTS['progress_hooks'].append(lambda d: youtube_download_hook(context, d))
    if ENABLED and flow.request.pretty_host.endswith(""):
        context.log(flow.request.pretty_host + flow.request.path)
        if '/stream' in flow.request.path:
            flow.reply(make_reply_stream(context, flow))
        elif 'q=' in flow.request.path:
            flow.reply(make_reply_query(context, flow))

If the request contains q=, Audiosurf is performing a query. The other option we handle is /stream which whill stream back the track:

def download_youtube_video(id):
    with youtube_dl.YoutubeDL(YDL_OPTS) as ydl:
        result = ydl.extract_info(''+id, download=True)
        video = result['entries'][0] if 'entries' in result else result
    return result, video

def youtube_download_hook(context, d):
    if d['status'] == 'finished':
        context.__filename = d['filename']

def make_reply_stream(context, flow):
    resp = HTTPResponse(b"HTTP/1.1", 200, b"OK",
            Headers(Content_Type="audio/mpeg"), b"")
    id = flow.request.path.split('/')[2]
    context.__filename = None
    result, video = download_youtube_video(id)
    while not context.__filename:
    resp.content = open(context.__filename, 'rb').read()
    return resp

def make_reply_query(context, flow):
    resp = HTTPResponse(b"HTTP/1.1", 200, b"OK",
            Headers(Content_Type="application/json"), b"")
    bits = urlparse(flow.request.path)
    query = parse_qs(bits.query)['q'][0]
    list = make_song_list(query, context)
    resp.content = json.dumps(list).encode('utf-8')
    return resp

Note that we use some youtube_dl hook trickery to make sure we wait until the file is downloaded. (youtube_dl will spawn a separate process.)

Finally, we need to implement two more functions:

def make_song_dict(id, title, artist, description='', artwork_url=None):
    d = copy.deepcopy(SOUNDCLOUD_TRACK_TEMPLATE)
    d['id'] = id
    d['title'] = title
    d['description'] = description
    d['attachments_uri'] = d['attachments_uri'].format(id)
    d['waveform_url'] = d['waveform_url'].format(id)
    d['artwork_url'] = d['artwork_url'].format(id)
    if artwork_url is not None:
        d['artwork_url'] = artwork_url
    d['stream_url'] = d['stream_url'].format(id)
    d['uri'] = d['uri'].format(id)
    d['user']['username'] = artist
    return d

def make_song_list(query, context):
    q = quote(query)
    vd = 'short' if YOUTUBE_SHORT_ONLY else 'any'
    url = '{q}&type=video&videoCategoryId=10&videoDuration={vd}&key={key}'.format(q=q, vd=vd, key=YOUTUBE_API_KEY)
    reply = requests.get(url).json()
    list = []
    for item in reply['items']:
        d = make_song_dict(item['id']['videoId'],
    return list

And we’re done! We can use Proxifier PE or another program to force connections from Audiosurf 2 to go through our man in the middle server. You can see the result in action here:

Final warning: this is meant to be a fun weekend experiment and in no way provided to circumvent YouTube or other streaming provider’s policies. Just as using youtube_dl, use this at your own risk!