Coder Social home page Coder Social logo

name 'vlc' not defined about python-vlc HOT 15 CLOSED

rhodso avatar rhodso commented on July 28, 2024
name 'vlc' not defined

from python-vlc.

Comments (15)

mrJean1 avatar mrJean1 commented on July 28, 2024

Try typing one of the following lines:

  python  vlc.py  -v

or if vlc.py was pip-installed:

  python  -m  vlc  -v

from python-vlc.

rhodso avatar rhodso commented on July 28, 2024

vlc.py was pip installed, here's the output:

vlc.py: 3.0.18121 (Wed Nov 16 12:04:29 2022 3.0.18)
libVLC: 3.0.18 Vetinari (0x3001200)
plugins: C:\Program Files\VideoLAN\VLC
Python: 3.10.2 (64bit) Windows 10

from python-vlc.

mrJean1 avatar mrJean1 commented on July 28, 2024

So, at least on Windows 10 vlc.py should work. Try running a video like

  python  -m  vlc  <video_file_name>

Then type ? to see more single-key options. After that, try the same 2 lines on Linux and that should provide some clue what's wrong.

from python-vlc.

rhodso avatar rhodso commented on July 28, 2024

I've not tried getting anything running on linux just yet. That'll (probably) be running on a raspberry pi on headless mode, or at least my laptop that has arch on it

Here's a screenshot that I took while the video was playing - I was trying to get it to recognise some input, but that didn't appeart to happen
output

Here's a screenshot that I took when the video ended. The issue of not recognising input is still there, so I'm unable to close the vlc player directly, but can close it by closing the terminal
image

from python-vlc.

mrJean1 avatar mrJean1 commented on July 28, 2024

The VLC.App must be installed and must be working properly on its own. Only then, python-vlc vlc.py can work … as long as vlc.py can find the libvlc.dll or -.so library included in the VLC.App.

from python-vlc.

rhodso avatar rhodso commented on July 28, 2024

VLC is installed and working fine:
image

from python-vlc.

mrJean1 avatar mrJean1 commented on July 28, 2024

This is Windows, isn't it? What about Linux and Raspberry? Does the plain VLC.App run on those?

Btw, the errors and not being able to quit are a vlc.py issue on Windows. Try using the tkvlc.py example with Python 3.11.2 on Windows. Download that from the python-vlc web site and use the command line:

  python  tkvlc.py  <video_file_name>

One final comment, make sure to use the 64-bit VLC.App with vlc.py in 64-bit Python. Or use the 32-bit version of both.

from python-vlc.

rhodso avatar rhodso commented on July 28, 2024

That appears to have worked
image

I'll go try the other stuff with python 3.11.2 and see what happens

from python-vlc.

rhodso avatar rhodso commented on July 28, 2024

Still getting the same error in the initial script I had the issue in

image

Using the following:
Python 3.11.2 (64 bit)
python-vlc 3.0.18121 (from pip install)
VLC Application 3.0.18 64 bit

I'm also still developing the script so I'm just concerned with getting it working on Windows for the moment, I'll deal with linux when I've actually got something I can test

I can try it on linux if you think that'll help, though

from python-vlc.

mrJean1 avatar mrJean1 commented on July 28, 2024

A NameError usually means undefined object. In the script core.py there a line somewhere like this one

  import vlc

or maybe -just a guess-

  from main import vlc

In any case, first get the script to work on one platform (Windows or Linux) before moving to the other and then yet an other.

from python-vlc.

rhodso avatar rhodso commented on July 28, 2024

Currently I have the following in core.py:

os.add_dll_directory(r"C:\\Program Files\\VideoLAN\\VLC")

# Some other code

try:
        logging.info(msg="Importing libraries...")
        # TODO: Add other needed libraries here
        import yt_dlp
        import vlc
 except ImportError:
        # Log which libraries failed to import
        logging.error(msg="Failed to import libraries: " + str(sys.exc_info()[1]))

# Some other code

def play_song(self, url):
        # Get the source url
        source_url = self.get_video(url)

        # Create the vlc instance
        vlc_instance = vlc.Instance()
        vlc_player = vlc_instance.media_player_new()
        
        # Create the media
        media = vlc_instance.media_new(source_url)

        # Set the media
        vlc_player.set_media(media)

        # Play the media
        vlc_player.play()

def test():
    c = core()
    c.play_song("https://www.youtube.com/watch?v=dQw4w9WgXcQ")

if __name__ == '__main__':
    test()

And then the output of that is just:

2023-03-25 00:04:04,310 Importing libraries...
Traceback (most recent call last):
  File "G:\!Programming\Python\HKMB2\py\core.py", line 126, in <module> 
    test()
  File "G:\!Programming\Python\HKMB2\py\core.py", line 123, in test     
    c.play_song("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
  File "G:\!Programming\Python\HKMB2\py\core.py", line 109, in play_song
    vlc_instance = vlc.Instance()
                   ^^^
NameError: name 'vlc' is not defined

So it's definetly importing the module, but when I go to create the instance it says it's not defined

from python-vlc.

mrJean1 avatar mrJean1 commented on July 28, 2024

It is hard to tell, since several pieces are missing.

It seems play_song is a method of some class. Where does it get vlc from?

In any case, this is not a VLC or vlc.py issue. Sorry.

from python-vlc.

rhodso avatar rhodso commented on July 28, 2024

Ok, so that works.
Full code for reference:

# Stanrdard library imports
import os
import logging

# Third party imports
import yt_dlp
import vlc

# Options for the ytdl object
ytdl_opts = {
    'format': 'bestaudio/best',
    'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
    'restrictfilenames': True,
    'noplaylist': True,
    'nocheckcertificate': True,
    'ignoreerrors': False,
    'logtostderr': False,
    'quiet': True,
    'no_warnings': True,
    'default_search': 'auto',
    'source_address': '0.0.0.0' # bind to all IPv4 addresses
}

# Create the YTDL object
ytdl = yt_dlp.YoutubeDL(ytdl_opts)

# Logging options
LOG_TO_FILE = False
LOG_FILE_NAME = "bot.log"

# Delete the log file if it exists
if os.path.isfile(path=LOG_FILE_NAME):
    os.remove(path=LOG_FILE_NAME)

# Set up logging
if LOG_TO_FILE:
    log_handler = logging.basicConfig(format='%(asctime)s %(message)s', filename=LOG_FILE_NAME, encoding='utf-8', level=logging.INFO)
else:
    log_handler = logging.basicConfig(format='%(asctime)s %(message)s', encoding='utf-8', level=logging.INFO)

# Get the source url from the video url
def get_video(url):
    info = ytdl.extract_info(url, download=False)

    formats = info['formats']

    # Get the best audio format
    for f in formats:
        if f.get('acodec') == 'opus':
            best_audio_format = f
            break
    
    # If we didn't find an opus format, just use the first one
    if not best_audio_format:
        best_audio_format = formats[0]

    # Get the source url
    source_url = best_audio_format['url']

    # Return the source url
    return source_url

# Play the song
def play_song(url):
    # Get the source url
    source_url = get_video(url)

    # Create the vlc instance
    vlc_instance = vlc.Instance()
    vlc_player = vlc_instance.media_player_new()
    
    # Create the media
    media = vlc_instance.media_new(source_url)

    # Set the media
    vlc_player.set_media(media)

    # Play the media
    vlc_player.play()

# Test here for now
if __name__ == '__main__':
    play_song("https://www.youtube.com/watch?v=dQw4w9WgXcQ")

from python-vlc.

mrJean1 avatar mrJean1 commented on July 28, 2024

Great.

from python-vlc.

rhodso avatar rhodso commented on July 28, 2024

I guess the solution then is to just try moving the imports around and hope lol

from python-vlc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.