extract stream title from the output of mplayer

Jabba Laci jabba.laci at gmail.com
Tue Mar 18 13:51:31 EDT 2014


> Python. (Or s/guess/hop/ if you prefer!) There are many ways this
> could be done; what have you tried, what partly worked, what did
> something unexpected?

Hi,

I managed to solve the problem. In the man of mplayer I found how to
quit after X seconds: "-endpos X". See my solution below.

Best,

Laszlo

============

import re
import shlex
from subprocess import PIPE, Popen

URL = 'http://relay2.slayradio.org:8000/'


def get_exitcode_stdout_stderr(cmd):
    """
    Execute the external command and get its exitcode, stdout and stderr.
    """
    args = shlex.split(cmd)

    proc = Popen(args, stdout=PIPE, stderr=PIPE)
    out, err = proc.communicate()
    exitcode = proc.returncode
    #
    return exitcode, out, err


def get_title():
    cmd = "mplayer -endpos 1 -ao null {url}".format(url=URL)
    out = get_exitcode_stdout_stderr(cmd)[1]

    for line in out.split("\n"):
#        print(line)
        if line.startswith('ICY Info:'):
            match = re.search(r"StreamTitle='(.*)';StreamUrl=", line)
            title = match.group(1)
            return title

def main():
    print(get_title())



More information about the Python-list mailing list