python调用vlc显示视频实例,开发基于VLC

2019-10-09 04:14 来源:未知

c#万能视频播放器(附代码)

在windows系统如果开发万能播放器,一般都是基本DirectShow来开发,开发也很简单,但缺点也很多,一个文件格式是否能够播放完全取决于你 是否安装了正确的解析器和解码器,即使现在有了万能解器安装包也会出现很多问题,不过自从很多开源软件的出现,比如大名鼎鼎的ffmpeg,vlc等,一 切都变得简单起来,基于ffmpeg sdk开发就不多介绍了,本文主要介绍基于 VLC SDK来开发自己的播放器,一切都是那么的简单.

python调用vlc显示视频实例
将libvlc.dll和libvlccore.dll放在sdk文件夹中,plugins文件夹也放在sdk文件夹中,sdk文件夹放在程序目录下。
我弄这个,实际上是为了推广,我在北京。有意者请加我微信,我的微商微信:xi9902
myvlc.py文件:
import ctypes
import os
class myvlc():
def init:
plugin_arg = r"--plugin-path=" + os.path.join(os.getcwd(), r'sdkplugins')
arguments = ["-I", "dummy", "--no-ignore-config", plugin_arg]
arguments = [bytes(a,"utf-8") for a in arguments]
p = os.getcwd()
os.chdir(os.path.join)
dll = ctypes.CDLL("libvlc.dll")
os.chdir
self.dll = dll
self.libvlc_instance_ = self.dll.libvlc_new(len(arguments),(ctypes.c_char_p * len(arguments))(*arguments))
self.libvlc_media_player_ = self.dll.libvlc_media_player_new(self.libvlc_instance_)
self.libvlc_media = None
def del:
self.Stop()
if(self.libvlc_media):
self.dll.libvlc_media_release(self.libvlc_media)
if(self.libvlc_media_player_):
self.dll.libvlc_media_player_release(self.libvlc_media_player_)
if(self.libvlc_instance_):
self.dll.libvlc_release(self.libvlc_instance_)
def SetRenderWindow(self,hwnd):#嵌入窗体
if(self.libvlc_instance_):
self.dll.libvlc_media_player_set_hwnd(self.libvlc_media_player_,hwnd)
def PlayFile(self,filePath):#播放文件
if(self.libvlc_media):
self.dll.libvlc_media_release(self.libvlc_media)
self.libvlc_media = None
self.libvlc_media = self.dll.libvlc_media_new_path(self.libvlc_instance_, filePath.encode
if(self.libvlc_media):
self.dll.libvlc_media_player_set_media(self.libvlc_media_player_, self.libvlc_media)
self.dll.libvlc_media_player_play(self.libvlc_media_player_)
def PlayUrl(self,filePath):#播放网络流
if(self.libvlc_media):
self.dll.libvlc_media_release(self.libvlc_media)
self.libvlc_media = None
self.libvlc_media = self.dll.libvlc_media_new_location(self.libvlc_instance_, filePath.encode
if(self.libvlc_media):
self.dll.libvlc_media_player_set_media(self.libvlc_media_player_, self.libvlc_media)
self.dll.libvlc_media_player_play(self.libvlc_media_player_)
def Stop:#停止播放
if(self.libvlc_media_player_):
self.dll.libvlc_media_player_stop(self.libvlc_media_player_)
if(self.libvlc_media):
self.dll.libvlc_media_release(self.libvlc_media)
self.libvlc_media = None

c#万能视频播放器

  本人之前很多的文章中均提到了使用libvlc为播放器内核制作的播放器,也许有些朋友对此感兴趣,于是我用c#写了一个调用libvlc api实现的万能视频播放器,与大家分享一下。说它“万能”,当然是因为我们站在了vlc的肩膀上。

  vlc是一个强大而且开源的多媒体播放器,也可以说是一个多媒体平台。它支持非常广泛的媒体格式的本地播放,完全可以媲美mplayer,其对视频网络流的处理能力更是非常强悍。libvlc就是指的vlc的核心,它向外提供了一系列的接口,通过接口,来实现视频播放等复杂的功能。libvlc对外提供了c语言的接口,也有其他语言,包括.net的绑定,在其官网上就有,不过已经“年久失修”。我之前用Qt, MFC实现过基于libvlc的播放器,不过鉴于园子里c#开发人员较多,遂用c#封装了一下libvlc的API接口,并实现了一个视频播放器。

  先上鉴赏图,外表很简单,不过,外表不是重点:)

 

    图片 1

  

  首先是libvlc的一些导出函数,我在注释里对它们的功能都有说明

图片 2;)

 1         // 创建一个libvlc实例,它是引用计数的 2         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
 3         [SuppressUnmanagedCodeSecurity]
 4         private static extern IntPtr libvlc_new(int argc, IntPtr argv);
 5 
 6         // 释放libvlc实例 7         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
 8         [SuppressUnmanagedCodeSecurity]
 9         public static extern void libvlc_release(IntPtr libvlc_instance);
10 
11         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
12         [SuppressUnmanagedCodeSecurity]
13         public static extern String libvlc_get_version();
14 
15         // 从视频来源(例如Url)构建一个libvlc_meida16         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
17         [SuppressUnmanagedCodeSecurity]
18         private static extern IntPtr libvlc_media_new_location(IntPtr libvlc_instance, IntPtr path);
19 
20         // 从本地文件路径构建一个libvlc_media21         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
22         [SuppressUnmanagedCodeSecurity]
23         private static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path);
24 
25         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
26         [SuppressUnmanagedCodeSecurity]
27         public static extern void libvlc_media_release(IntPtr libvlc_media_inst);
28 
29         // 创建libvlc_media_player(播放核心)30         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
31         [SuppressUnmanagedCodeSecurity]
32         public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance);
33 
34         // 将视频(libvlc_media)绑定到播放器上35         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
36         [SuppressUnmanagedCodeSecurity]
37         public static extern void libvlc_media_player_set_media(IntPtr libvlc_media_player, IntPtr libvlc_media);
38 
39         // 设置图像输出的窗口40         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
41         [SuppressUnmanagedCodeSecurity]
42         public static extern void libvlc_media_player_set_hwnd(IntPtr libvlc_mediaplayer, Int32 drawable);
43 
44         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
45         [SuppressUnmanagedCodeSecurity]
46         public static extern void libvlc_media_player_play(IntPtr libvlc_mediaplayer);
47 
48         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
49         [SuppressUnmanagedCodeSecurity]
50         public static extern void libvlc_media_player_pause(IntPtr libvlc_mediaplayer);
51 
52         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
53         [SuppressUnmanagedCodeSecurity]
54         public static extern void libvlc_media_player_stop(IntPtr libvlc_mediaplayer);
55 
56         // 解析视频资源的媒体信息(如时长等)57         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
58         [SuppressUnmanagedCodeSecurity]
59         public static extern void libvlc_media_parse(IntPtr libvlc_media);
60 
61         // 返回视频的时长(必须先调用libvlc_media_parse之后,该函数才会生效)62         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
63         [SuppressUnmanagedCodeSecurity]
64         public static extern Int64 libvlc_media_get_duration(IntPtr libvlc_media);
65 
66         // 当前播放的时间67         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
68         [SuppressUnmanagedCodeSecurity]
69         public static extern Int64 libvlc_media_player_get_time(IntPtr libvlc_mediaplayer);
70 
71         // 设置播放位置(拖动)72         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
73         [SuppressUnmanagedCodeSecurity]
74         public static extern void libvlc_media_player_set_time(IntPtr libvlc_mediaplayer, Int64 time);
75 
76         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
77         [SuppressUnmanagedCodeSecurity]
78         public static extern void libvlc_media_player_release(IntPtr libvlc_mediaplayer);
79 
80         // 获取和设置音量81         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
82         [SuppressUnmanagedCodeSecurity]
83         public static extern int libvlc_audio_get_volume(IntPtr libvlc_media_player);
84 
85         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
86         [SuppressUnmanagedCodeSecurity]
87         public static extern void libvlc_audio_set_volume(IntPtr libvlc_media_player, int volume);
88 
89         // 设置全屏90         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
91         [SuppressUnmanagedCodeSecurity]
92         public static extern void libvlc_set_fullscreen(IntPtr libvlc_media_player, int isFullScreen);

图片 3;)

  

  要使用libvlc api进行播放,首先需要创建一个libvlc的实例,就是lbvlc_instance,之后所有的操作都是基于这个实例来完成。

  每一个播放窗口,对应一个libvlc_media_player,而每一个媒体文件,就是一个libvlc_media。所以,调用的步骤就是:

    1. 创建libvlc_instance
    2. 创建libvlc_media_player
    3. 开始播放时,创建libvlc_media,通知libvlc_media_player要播放的媒体文件,之后就可以释放libvlc_media了
    4. 操作libvlc_media_player,实现播放、暂停等功能

  下面对这些操作进行封装,以供上层使用。

 

class VlcPlayer
     {
         private IntPtr libvlc_instance_;
         private IntPtr libvlc_media_player_;

         private double duration_;

         public VlcPlayer(string pluginPath)
         {
             string plugin_arg = "--plugin-path=" + pluginPath;
             string[] arguments = { "-I", "dummy", "--ignore-config", "--no-video-title", plugin_arg };
             libvlc_instance_ = LibVlcAPI.libvlc_new(arguments);

             libvlc_media_player_ = LibVlcAPI.libvlc_media_player_new(libvlc_instance_);
         }

         public void SetRenderWindow(int wndHandle)
         {
             if (libvlc_instance_ != IntPtr.Zero && wndHandle != 0)
             {
                 LibVlcAPI.libvlc_media_player_set_hwnd(libvlc_media_player_, wndHandle);
             }
         }

         public void PlayFile(string filePath)
         {
             IntPtr libvlc_media = LibVlcAPI.libvlc_media_new_path(libvlc_instance_, filePath);
             if (libvlc_media != IntPtr.Zero)
             {
                 LibVlcAPI.libvlc_media_parse(libvlc_media);
                 duration_ = LibVlcAPI.libvlc_media_get_duration(libvlc_media) / 1000.0;

                 LibVlcAPI.libvlc_media_player_set_media(libvlc_media_player_, libvlc_media);
                 LibVlcAPI.libvlc_media_release(libvlc_media);

                 LibVlcAPI.libvlc_media_player_play(libvlc_media_player_);
             }
         }

         public void Pause()
         {
             if (libvlc_media_player_ != IntPtr.Zero)
             {
                 LibVlcAPI.libvlc_media_player_pause(libvlc_media_player_);
             }
         }

         public void Stop()
         {
             if (libvlc_media_player_ != IntPtr.Zero)
             {
                 LibVlcAPI.libvlc_media_player_stop(libvlc_media_player_);
             }
         }

         public double GetPlayTime()
         {
             return LibVlcAPI.libvlc_media_player_get_time(libvlc_media_player_) / 1000.0;
         }

         public void SetPlayTime(double seekTime)
         {
             LibVlcAPI.libvlc_media_player_set_time(libvlc_media_player_, (Int64)(seekTime * 1000));
         }

         public int GetVolume()
         {
             return LibVlcAPI.libvlc_audio_get_volume(libvlc_media_player_);
         }

         public void SetVolume(int volume)
         {
             LibVlcAPI.libvlc_audio_set_volume(libvlc_media_player_, volume);
         }

         public void SetFullScreen(bool istrue)
         {
             LibVlcAPI.libvlc_set_fullscreen(libvlc_media_player_, istrue ? 1 : 0);
         }

         public double Duration()
         {
             return duration_;
         }

         public string Version()
         {
             return LibVlcAPI.libvlc_get_version();
         }
     }

 

  封装为VlcPlayer之后,调用就会变得非常简单。

  • 首先确定好vlc的插件目录
  • 在你的代码里创建VlcPlayer对象
  • 调用SetRenderWindow,以设置视频显示的窗口,否则会变成一个独立的播放窗口
  • 调用PlayFile,即可开始播放

    图片 4

 

  程序做好之后,需要带上libvlc.dlllibvlccore.dll,这两个是vlc的播放内核,因为vlc把编解码和格式解析的支持设计成了插件的形式,所以还必须要带上vlc的plugins目录里的插件。

  我把整个工程打包(包括libvlc.dlllibvlccore.dll)上传到了博客上,点击这里,就可以下载。

  plugins目录文件有点大(因为libvlc支持的格式和功能非常多,如果你不需要,删掉对应插件即可),我放到了115网盘,下载地址是。下载后放到“bin/Debug”目录即可调试运行。

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++

  图片 5HaibinDev软件工作室。(版权所有,转载请注明作者和出处~)

+++++++++++++++++++++++++++++++++++++++++++++++++++++

 

调用方法:
import myvlc

1.先下载VLC player, ,  安装的时候要安装开发库(SDK)

v=myvlc.myvlc()
self.v=v
v.SetRenderWindow(ctypes.c_void_p(int(self.pushButton_8.winId
v.PlayFile(r'F:111.wmv')

2.用VC 6.0/2003/2005/2008/2010 IDE均可开发自己的测试程序

3.创建一个控制台程序,当然创建一个窗口程序更好,由于只是为了关注于如何使用VLC SDK,所以都无所谓的

4.在工程中设置好vlc的 include和lib路径,一般VLC sdk路径是C:Program FilesVideoLANVLCsdk

5.复制libvlc.dll, libvlccore.dll,plugins目录到工程debug目录,如果是release需要复制到release目录,目录结构如图:

            图片 6

之后 就是完整的代码:

 

#include <stdio.h>

#include <tchar.h>

#include <time.h>

#include <windows.h>

 

#include <vlc/vlc.h>

#pragma comment(lib, "libvlc.lib")

#pragma comment(lib, "libvlccore.lib")

 

int main(int argc, char* argv[])

{

libvlc_instance_t *     vlc_ins    = NULL;

libvlc_media_player_t * vlc_player = NULL;

libvlc_media_t *        vlc_media  = NULL;

 

const char * vlc_args[] = 

{

"-I",

"dummy",

"--ignore-config",

"--extraintf=logger",

"--verbose=2", 

};  

TAG标签:
版权声明:本文由金沙澳门官网4166发布于世界史,转载请注明出处:python调用vlc显示视频实例,开发基于VLC