基于Libvlc库的视频录制功能简述(C#)
最近在学习libvlc库,如下讲述如何播放网络或本地视频并进行视频录制的功能:
一、先看效果:
1-1、主界面:首先Init环境初始化,然后可选择播放网络流或是本地视频文件;1-2、若播放网络流,则在Network左边的textbox中输入网络流地址,并通过Select Save Path选择录像的存储目录;
1-3、点击REC即可进行录制,录制存储的地址即选择的存储目录,如下图:
1-4、点击RECing...后,可在存储目录下找到刚才存储的视频;
1-5、也可点击Location进行本地视频播放;
二、详细:
2-1:需要使用带有视频录制的libvlc库,可参考:https://download.****.net/download/avsuper/9919212;
2-2:MediaPlyaer接口编写,可参考:https://blog.****.net/lassewang/article/details/52240894,但是需要进行改写,如下:
①导入库函数进行添加:
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)]
internal static extern int libvlc_media_player_recorder_start(libvlc_media_player_t libvlc_media_player, IntPtr path);
internal static extern int libvlc_media_player_recorder_start(libvlc_media_player_t libvlc_media_player, IntPtr path);
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern int libvlc_media_player_recorder_stop(libvlc_media_player_t libvlc_media_player);
internal static extern int libvlc_media_player_recorder_stop(libvlc_media_player_t libvlc_media_player);
如果喜欢改的话还可添加另一个函数(对于本例可不添加):
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]internal static extern void libvlc_media_add_option(libvlc_media_t p_md, String psz_option);
②共有函数中进行添加功能函数:
/// <summary>
/// 视频存储开始
/// </summary>
/// <param name="libvlc_media_player"></param>
/// <param name="url"></param>
public static void Save_MediaPlayer(libvlc_media_player_t libvlc_media_player, string url)
{
IntPtr pMrl = IntPtr.Zero;
/// 视频存储开始
/// </summary>
/// <param name="libvlc_media_player"></param>
/// <param name="url"></param>
public static void Save_MediaPlayer(libvlc_media_player_t libvlc_media_player, string url)
{
IntPtr pMrl = IntPtr.Zero;
try
{
if (url == null ||
libvlc_media_player == IntPtr.Zero ||
libvlc_media_player == null)
{
return;
}
{
if (url == null ||
libvlc_media_player == IntPtr.Zero ||
libvlc_media_player == null)
{
return;
}
pMrl = StrToIntPtr(url);
if (pMrl == null || pMrl == IntPtr.Zero)
{
return;
}
if (pMrl == null || pMrl == IntPtr.Zero)
{
return;
}
SafeNativeMethods.libvlc_media_player_recorder_start(libvlc_media_player, pMrl);
}
catch (Exception)
{
}
}
}
catch (Exception)
{
}
}
/// <summary>
/// 停止录像功能,存储
/// </summary>
/// <param name="libvlc_media_player"></param>
public static void UnSave_MediaPlayer(libvlc_media_player_t libvlc_media_player)
{
try
{
if (libvlc_media_player == IntPtr.Zero ||
libvlc_media_player == null)
{
return;
}
SafeNativeMethods.libvlc_media_player_recorder_stop(libvlc_media_player);
}
catch (Exception)
{
/// 停止录像功能,存储
/// </summary>
/// <param name="libvlc_media_player"></param>
public static void UnSave_MediaPlayer(libvlc_media_player_t libvlc_media_player)
{
try
{
if (libvlc_media_player == IntPtr.Zero ||
libvlc_media_player == null)
{
return;
}
SafeNativeMethods.libvlc_media_player_recorder_stop(libvlc_media_player);
}
catch (Exception)
{
throw;
}
}
}
}
这样就可以使用,libvlc的录像功能了,只需调用Save_MediaPlaye和UnSave_MediaPlaye即可,(注:调用Save_MediaPlaye后并不会立即出现存储的视频,调用UnSave_MediaPlaye后就会在存储视频的地址找到存储好的视频)。
第一次写,有很多“规矩不了解,以后逐步进行完善。
源码:https://download.****.net/download/hopeless123/10311625