Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VLC Media Player API

I am trying to stream an online video using VLC media player. But the URL I'm receiving is random, so that URL needs to be linked to the VLC media player online stream. Is there any APIs that enables the random online video to be played ?

A small understanding of the project that I'm building...

  1. I have a device that will receive the URL from the server and will play it on a screen.

  2. Earlier I was playing it via a web browser. But this time I want it to implement it using a media player.Thus my question, is there any API for VLC media player that can be used to stream online videos?

*** BTW I'm using python to write my scripts.

like image 318
askd Avatar asked Sep 07 '25 11:09

askd


2 Answers

As wizzwizz4 stated the vlc.py program is available @ https://wiki.videolan.org/Python_bindings along with example code for wxpython, pyQt and pyGtk
The documentation is here: https://www.olivieraubert.net/vlc/python-ctypes/doc/

Here is a rough bit of code, which runs from the command line, to give you a start

import requests
import vlc
import time
import os

#Test data a local file, a couple of radio stations and a video to cover all the bases
urls = [
    'file:///home/rolf/Music/H5O.mp3',
    'http://www.lounge-radio.com/listen128.m3u',
    'http://network.absoluteradio.co.uk/core/audio/aacplus/live.pls?service=acbb',
    'http://statslive.infomaniak.ch/playlist/energy90s/energy90s-high.mp3/playlist.pls',
    'http://streaming.radio.rtl2.fr/rtl2-1-44-128',
    'http://clips.vorwaerts-gmbh.de/VfE_html5.mp4'
    ]

#Define playlist extensions
playlists = set(['pls','m3u'])

#Define vlc playing Status values
playing = set([1,2,3,4])

Instance = vlc.Instance()

# Loop over urls provided
for url in urls:
    print ("\n\nLooking for:", url)
    # Grab file extension
    ext = (url.rpartition(".")[2])[:3]

    found = False
    # Test if url is a local file or remote
    if url[:4] == 'file':
        if os.path.isfile(url[7:]):
            found = True
        else:
            print ('Error: File ', url[7:], ' Not found')
            continue
    else:
        try:
            r = requests.get(url, stream=True)
            found = r.ok
        except ConnectionError as e:
            print('failed to get stream: {e}'.format(e=e))
            continue
    if found:
        player = Instance.media_player_new()
        Media = Instance.media_new(url)
        Media_list = Instance.media_list_new([url])
        Media.get_mrl()
        player.set_media(Media)

        # if url is a playlist load list_player else use player
        if ext in playlists:
            list_player = Instance.media_list_player_new()
            list_player.set_media_list(Media_list)
            if list_player.play() == -1:
                print ("\nError playing playlist")
        else:
            if player.play() == -1:
                print ("\nError playing Stream")

#=========================================================#
#        #Use this code for 15 second samples
        print ('Sampling ', url, ' for 15 seconds')
        time.sleep(15)
#
#=========================================================#

#        #Use this code to play audio until it stops
#        print ('Playing ', url, ' until it stops')
#        time.sleep(5) #Give it time to get going
#        while True:
#            if ext in playlists:
#                state = list_player.get_state()
#                if state not in playing:
#                    break
#            else:
#                state = player.get_state()
#                if state not in playing:
#                    break
#=========================================================#

        if ext in playlists:
            list_player.stop()
        else:
            player.stop()

    else:
        print ('\nError getting the audio')
like image 107
Rolf of Saxony Avatar answered Sep 10 '25 00:09

Rolf of Saxony


If the URL is to the stream / video file, you can just open it directly in VLC like you would anything else. If it's to an HTML document, you'll need to extract the URL of the actual stream.

like image 25
wizzwizz4 Avatar answered Sep 10 '25 01:09

wizzwizz4