1

I run the following command in a linux terminal:

vlc http://streamx/live/....stream/playlist.m3u8 --rate=1 --video-filter=scene --vout=dummy --run-time=3 --scene-format=png --scene-ratio=24 --scene-path=/home/pi/Desktop vlc://quit

If the url is okay, it makes some pictures from streams. I would like to know if the command ran successfully or not.

if the url is not correct is writes out:

[73b00508] core input error: open of 'http://streamx/live/....stream/playlist.m3u8' failed
[73b00508] core input error: Your input can't be opened
[73b00508] core input error: VLC is unable to open the MRL 'http://streamx/live/....stream/playlist.m3u8'. Check the log for details.

if the url is correct is writes out:

[73b03f20] httplive stream: HTTP Live Streaming (streamx/live/....stream/playlist.m3u8)

How can I get after running the command (for example in a python script) if the url was okay or not?

Thanks in advance!

Marci
  • 427
  • 2
  • 9
  • 20
  • Use something like `wget` to check that you get a valid response (ie. not 500 or 404 etc) and that the response encoding is of the type you want before actually opening VLC. – bated Sep 13 '17 at 18:44

1 Answers1

2

We need to check two things.

  • 1) If the URL itself is alive
  • 2) If the URL is alive, is the data streaming (you may have broken link).

1) To check if URL is alive. We can check status code. Anything 2xx or 3xx is good (you can tailor this to your needs).

import urllib
url = 'http://aska.ru-hoster.com:8053/autodj'

code = urllib.urlopen(url).getcode()
 if str(code).startswith('2') or str(code).startswith('3') :
    print 'Stream is working'
else:
    print 'Stream is dead'

2) Now we have good URL , but we need to check if we have streaming and link is not dead.
Using VLC, we can connect to site, try to play the media at the link and then check for errors.

Here is a working example that I have from my other posting.

import vlc
import time

url = 'http://aska.ru-hoster.com:8053/autodj'
#define VLC instance
instance = vlc.Instance('--input-repeat=-1', '--fullscreen')

#Define VLC player
player=instance.media_player_new()

#Define VLC media
media=instance.media_new(url)

#Set player media
player.set_media(media)

#Play the media
player.play()


#Sleep for 5 sec for VLC to complete retries.
time.sleep(5)
#Get current state.
state = str(player.get_state())

#Find out if stream is working.
if state == "vlc.State.Error" or state == "State.Error":
    print 'Stream is dead. Current state = {}'.format(state)
    player.stop()
else:
    print 'Stream is working. Current state = {}'.format(state)
    player.stop()
Anil_M
  • 10,893
  • 6
  • 47
  • 74
  • Thanks for the quick answer, i'll definetly check this out! – Marci Sep 13 '17 at 18:19
  • Let me know if it works out. If it does, do consider up-voting and accepting answer to close the loop. If not let me know issues so that I can revisit problem. – Anil_M Sep 13 '17 at 18:21
  • 1) is working in python 2, but when I try in python 3: import urllib.request with urllib.request.urlopen(url) as url: s = url.read() the shell writes out nothing. Could you include a python 3 solution? Thanks – Marci Sep 13 '17 at 21:01
  • For python3, add `import urllib.request` and change `code=` with `code = urllib.request.urlopen(url).getcode()` see if it works. – Anil_M Sep 13 '17 at 21:09
  • working stream is okay, but I get urllib.error.HTTPError: HTTP Error 404: Not Found when stream is down – Marci Sep 13 '17 at 21:19
  • Can you provide website that shows stream is down. Code I provided does not cover all exceptions. – Anil_M Sep 13 '17 at 21:21
  • import urllib.request url = 'http://stream.e-cityguide.eu:1935/live/D1517560.stream/playlist.m3u8' try: code = urllib.request.urlopen(url).getcode() except urllib.error.HTTPError: code = 404 if str(code).startswith('2') or str(code).startswith('3') : print('Stream is working') else: print('Stream is dead') – Anil_M Sep 13 '17 at 21:36
  • Thanks for the tons of help, but are you sure that the only error that can happen is HttpError? thx – Marci Sep 13 '17 at 21:53
  • It catches any HTTPError, 404 is just a fake code to say there is error. – Anil_M Sep 13 '17 at 21:55
  • yeah, I understand, but is there a chance that a mistake occurs that is not HTTPError or is it almost impossible, right? – Marci Sep 13 '17 at 21:57
  • HTTPError is blanket error which should cover most errors. You will have to deal with others as they arise. You can put in another question when they do. – Anil_M Sep 13 '17 at 21:59
  • Hi, can I have a question? I have a link that is alive, but your code says it's dead. – Marci Dec 05 '17 at 15:52
  • Check return status for `code` .anything that doesn't start with 2 or 3 will display as dead. You will need to modify code to suit your requirements. – Anil_M Dec 06 '17 at 03:53