The VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. VLC is available for desktop operating systems and mobile platforms, such as Android, iOS, iPadOS, Tizen, Windows 10 Mobile, and Windows Phone.
We can use the VLC media player with the help of python as well, in order to install vlc module in python we will use the command given below
pip install python-vlc
Note: In order to use the vlc module in python, the user system should have vlc media player already installed on the machine.
Importing the VLC Module:
To import the VLC module use the following
import vlc
Fixing error that may occur while importing vlc module
1. If path is not added, the problem is that libvlc.dll is not in the PATH(System Variable). Just add the file libvlc.dll path to system variable, this file can be found in the vlc folder where it is installed
2. Wrong version of VLC, oftenly people download 32bits vlc’s version. This may cause some trouble if we have installed the 64 bits version of python. To fix that, we just need to reinstall the 64 bits vlc’s version.
3. Import os module prior to vlc module and register libvlc.dll using os.add_dll_directory(r’C:\Program Files\VideoLAN\VLC’).
Example 1: Playing Video using VLC
Python3
# importing vlc module import vlc # creating vlc media player object media = vlc.MediaPlayer( "1.mp4" ) # start playing video media.play() |
Output :
Example 2: Here we will derive the duration of a video file using the VLC module.
Python3
# importing time and vlc import time, vlc # method to play video def video(source): # creating a vlc instance vlc_instance = vlc.Instance() # creating a media player player = vlc_instance.media_player_new() # creating a media media = vlc_instance.media_new(source) # setting media to the player player.set_media(media) # play the video player.play() # wait time time.sleep( 0.5 ) # getting the duration of the video duration = player.get_length() # printing the duration of the video print ( "Duration : " + str (duration)) # call the video method video( "your_video.mp4" ) |
Output :
Duration : 5006