📜  Python中的 VLC 模块——简介

📅  最后修改于: 2022-05-13 01:54:23.310000             🧑  作者: Mango

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

示例 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