I have the following class which is imported by a main app.py, it is a webapp for a LAN which creates an RTP stream on an IP given in a config.ini file. Currently MP3 files work fine however MP4 files have no playout. Below is the class which is imported by app.py which creates the stream. Two variables being the file and the channel.
import vlc
import configparser
class VLCPlayer:
def __init__(self):
self.instance = vlc.Instance("--no-xlib")
#play media on parsed URL from config.ini
def play_media(self, media_path, channel_url):
media = self.instance.media_new(media_path)
media.add_option(f'sout=#rtp{{dst={channel_url}}}')
player = self.instance.media_player_new()
player.set_media(media)
player.play()
return player
#stop playback if there is a playout on the current channel
def stop_playback(self, player):
player.stop()
class ChannelManager:
def __init__(self, player):
self.player = player
self.channels = {}
def select_channel(self, channel, file_path):
# Parse the config.ini file
config = configparser.ConfigParser()
config.read('config.ini')
# Get the channel IP from config.ini
channel_url = config.get('Channels', channel, fallback=None)
# Check if the address is valid
if channel_url:
if channel in self.channels:
self.player.stop_playback(self.channels[channel])
self.channels[channel] = self.player.play_media(file_path, channel_url)
else:
# Handle error case if channel not found in config or other (else) error
raise ValueError("Channel not found or unavailable")
vlc_player = VLCPlayer()
channel_manager = ChannelManager(vlc_player)
I have tried adding ports into the config.ini, however these get appended with some default port "5004", such as when the command is sent to vlc after parsing, it cannot start an rtp playout due to there being two ports. I have tried multiple files and only mp3 types work. Is there any specification as to selecting encoding formats manually etc? Below is the section of app.py which calls the vlc integration class above. Any help is greatly appreciated as this is my first webapp that delves into python-vlc.
from flask import Flask, render_template, request, jsonify #redirect
from werkzeug.utils import secure_filename
import subprocess
import os
import configparser
import vlc_integration # Assuming vlc_integration.py is in the same directory
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
# Create an instance of VLCPlayer and ChannelManager at the beginning
vlc_player = vlc_integration.VLCPlayer()
channel_manager = vlc_integration.ChannelManager(vlc_player)
'''
Excluding other functions such as index and upload page. !!!
'''
#SERVING3.0
@app.route('/contentmanager', methods=['GET', 'POST'])
def content_manager():
if request.method == 'POST':
channel = request.form.get('Channel-Selection')
file = request.form.get('File-Selection')
if not channel or not file:
return jsonify(error="Invalid selection. Please select both a channel and a file."), 400
config = configparser.ConfigParser()
try:
config.read('config.ini')
if not config.has_option('Channels', channel):
print(channel)
return jsonify(error="Invalid channel. Please select a valid channel."), 400
except configparser.Error as e:
return jsonify(error="Error reading configuration file: " + str(e)), 500
file_path = os.path.join('uploads', file)
if not os.path.isfile(file_path):
return jsonify(error="File not found: " + file), 404
try:
channel_manager.select_channel(channel, file_path)
except subprocess.CalledProcessError as e:
return jsonify(error="Error initiating streaming: " + str(e)), 500
files = os.listdir('uploads')
return render_template('contentmanager.html', files=files)
# Switch flask.escape module for deployment to prevent depracated and broken features
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8088, debug=False)
'''