c++ - 如何使用 libvlc 只播放视频文件的音频?

标签 c++ libvlc

我想使用libvlc仅播放视频文件的音频。我怎样才能做到呢?

这是我的代码:

#include <vlc/vlc.h>

#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include <vector>

int main()
{
    libvlc_instance_t *inst = libvlc_new(0, nullptr);
    char const *location = "mario_00.webm";
    libvlc_media_t *vlc_media = libvlc_media_new_path(inst, location);

    libvlc_media_player_t *vlc_player = libvlc_media_player_new_from_media(vlc_media);
    libvlc_media_player_play(vlc_player); //this line will play the video and audio

    while(1){
        if(libvlc_media_get_state(vlc_media) == libvlc_Ended){
            break;
        }
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }

    libvlc_media_player_release(vlc_player);
    libvlc_media_release(vlc_media);
    libvlc_release(inst);
}

最佳答案

您可以使用libvlc_new()指定选项--no-video论据。

它的声明是

libvlc_instance_t* libvlc_new( int argc, const char *const *argv )

所以,它会是这样的:

const char* argv[] = { "--no-video" };

libvlc_instance_t *inst = libvlc_new( 1, argv );

另一种选择,如本thread中提到的,是选项--vout none。这样,代码将是:

const char* argv[] = { "--vout", "none" };

libvlc_instance_t *inst = libvlc_new( 2, argv );

但是,在播放媒体(音频)时,您会收到如下连续错误流:

[00007f8da808b7f0] main video output error: video output creation failed
[00007f8dc741e930] main decoder error: failed to create video output
[00007f8da80d2250] main video output error: video output creation failed
[00007f8dc741e930] main decoder error: failed to create video output
[00007f8da80d2250] main video output error: video output creation failed
[00007f8dc741e930] main decoder error: failed to create video output
[h264 @ 0x7f8dc74422e0] get_buffer() failed
[h264 @ 0x7f8dc74422e0] thread_get_buffer() failed
[h264 @ 0x7f8dc74422e0] decode_slice_header error
[h264 @ 0x7f8dc74422e0] no frame!
[00007f8da4045f80] main video output error: video output creation failed
[00007f8dc741e930] main decoder error: failed to create video output
[h264 @ 0x7f8dc7453f60] get_buffer() failed
[h264 @ 0x7f8dc7453f60] thread_get_buffer() failed
[h264 @ 0x7f8dc7453f60] decode_slice_header error
[h264 @ 0x7f8dc7453f60] no frame!
[00007f8d9c045f80] main video output error: video output creation failed
[00007f8dc741e930] main decoder error: failed to create video output
[h264 @ 0x7f8dc7499c40] get_buffer() failed
[h264 @ 0x7f8dc7499c40] thread_get_buffer() failed
[h264 @ 0x7f8dc7499c40] decode_slice_header error
[h264 @ 0x7f8dc7499c40] no frame!

使用 libvlc_media_player_set_nsobject() 也可以实现同样的效果像这样:

libvlc_media_player_set_nsobject( vlc_player, nullptr );

在这种情况下,您不必将 argcargv 传递给 libvlc_new()

关于c++ - 如何使用 libvlc 只播放视频文件的音频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61537188/

相关文章:

java - 从 vlcj 播放器数组中播放视频

c++ - 给定 N-1 约束,计算小于整数 N 的数字的可能排列数

c++ - 使用 static、const、constexpr 的全局声明/初始化

android - 在 windows 平台上使用 libVLC for Android

electron - WebChimera和wcjs没有在Ubuntu 16.04上预构建的视频输出

c# - 如何使用 VLC 流式传输图像/文件?

c++ - 我可以使用 EXPECT_CALL 来验证模拟对象的构造函数是否在特定时间调用成员函数吗?

c++ - 如何以 STL 方式将一定数量的字符从文件复制到 vector ?

c++ - std::bind() - 从派生类的成员函数中获取基保护成员函数

c - 将原始音频写入文件?