C++利用VLC库制作音视频的示例代码

  #include

  #include "vlc.h"

  #include

  //编码转换

  #include

  std::string Unicode2Utf8(const std::wstring& strIn)

  {

  std::string str;

  int length = ::WideCharToMultiByte(CP_UTF8, 0, strIn.c_str(), strIn.size(), NULL, 0, NULL, NULL);

  str.resize(length + 1);

  ::WideCharToMultiByte(CP_UTF8, 0, strIn.c_str(), strIn.size(), (LPSTR)str.c_str(), length, NULL, NULL);

  return str;

  }

  int main()

  {

  int argc = 1;

  char* argv[2];

  argv[0] = (char*)"--ignore-config"; //忽略配置

  libvlc_instance_t* vlc_ins = libvlc_new(argc, argv); //①创建实例

  std::string path = Unicode2Utf8(L"file:///D:C++ProjectVideoPlayVideoPlay音乐.mp4");

  libvlc_media_t* media = libvlc_media_new_location(vlc_ins, path.c_str());

  //libvlc_media_t* media = libvlc_media_new_path(vlc_ins, "音乐.mp4"); //②创建媒体

  //media = libvlc_media_new_location(vlc_ins, "file:///D:C++ProjectVideoPlayVideoPlay音乐.mp4");

  libvlc_media_player_t* player = libvlc_media_player_new_from_media(media); //③创建播放器

  do

  {

  int ret = libvlc_media_player_play(player); //④播放 返回值0开始 -1错误

  if (ret == -1) {

  printf("error found!

  ");

  break;

  }

  int vol = -1;

  while (vol == -1)

  {

  Sleep(10);

  vol = libvlc_audio_get_volume(player); //获取音量 默认100

  }

  //只有media解析加载完成,才会有下面的参数

  printf("volume is %d

  ", vol);

  libvlc_audio_set_volume(player, 10); //将音量设置到10

  libvlc_time_t tm = libvlc_media_player_get_length(player); //获取长度(毫秒数)

  printf("%02d:%02d:%02d

  ", int(tm / 1000 / 3600), int((tm / 1000 / 60) % 60), int(tm / 1000 % 60));

  int width = libvlc_video_get_width(player); //获取宽和高

  int height = libvlc_video_get_height(player);

  printf("width = %d height = %d

  ", width, height);

  while (!_kbhit()) { //检验键盘是否按下 按下为1

  printf("%f%%

  ", 100.0 * libvlc_media_player_get_position(player)); //获取播放进度

  Sleep(500);

  }

  getchar();

  libvlc_media_player_pause(player); //暂停

  getchar();

  libvlc_media_player_play(player); //继续

  getchar();

  libvlc_media_player_stop(player); //停止

  } while (0);

  libvlc_media_player_release(player); //⑤释放播放器

  libvlc_media_release(media); //⑥释放媒体

  libvlc_release(vlc_ins); //⑦释放实例

  return 0;

  }