VLC Media Player API

时间:2018-07-26 10:46:44

标签: python video media vlc

我正在尝试使用VLC媒体播放器流式传输在线视频。但是我收到的URL是随机的,因此该URL需要链接到VLC媒体播放器在线流。是否有任何API可以播放随机在线视频?

对我正在构建的项目有一点了解...

  1. 我有一台将从服务器接收URL并将在屏幕上播放的设备。

  2. 我以前是通过网络浏览器播放它的。但是这一次我希望它使用媒体播放器来实现。因此,我的问题是,是否有用于VLC媒体播放器的API可以用于流传输在线视频?

***顺便说一句,我正在使用python编写脚本。

2 个答案:

答案 0 :(得分:0)

如果该URL指向流/视频文件,则可以像其他任何操作一样直接在VLC中打开它。如果是HTML文档,则需要提取实际流的URL。

答案 1 :(得分:0)

正如wizzwizz4所说,vlc.py程序可在https://wiki.videolan.org/Python_bindings处使用,以及wxpython,pyQt和pyGtk的示例代码
文档在这里:https://www.olivieraubert.net/vlc/python-ctypes/doc/

这是从命令行运行的大致代码,为您提供一个开始

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')