Python中的 VLC 模块——简介
VLC媒体播放器是VideoLAN项目开发的一款免费开源的便携式跨平台媒体播放软件和流媒体服务器。 VLC 适用于桌面操作系统和移动平台,例如 Android、iOS、iPadOS、Tizen、Windows 10 Mobile 和 Windows Phone。
我们也可以在Python的帮助下使用 VLC 媒体播放器,为了在Python中安装 vlc 模块,我们将使用下面给出的命令
pip install python-vlc
注意:为了在Python中使用 vlc 模块,用户系统应该已经在机器上安装了 vlc 媒体播放器。
导入 VLC 模块:
要导入 VLC 模块,请使用以下命令
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’).
示例 1:使用 VLC 播放视频
Python3
# importing vlc module
import vlc
# creating vlc media player object
media = vlc.MediaPlayer("1.mp4")
# start playing video
media.play()
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")
输出 :
示例 2:这里我们将使用 VLC 模块导出视频文件的持续时间。
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")
输出 :
Duration : 5006