mardi 18 mars 2014

Re: extract stream title from the output of mplayer topic




> 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 http://www.velocityreviews.com/forums/t969837-re-extract-stream-title-from-the-output-of-mplayer.html".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())





Aucun commentaire:

Enregistrer un commentaire