简体   繁体   English

我怎么知道音频文件的格式?

[英]How do I know the format of audio file?

I want to make a music player and I want to filter out files not audio file on opening. 我想制作一个音乐播放器,并且要在打开时过滤掉文件而不是音频文件。
Should I use QAudioDecoder? 我应该使用QAudioDecoder吗?
But every file I checked with QAudioDecoder tells me the same codec "audio/pcm". 但是我用QAudioDecoder检查的每个文件都告诉我相同的编解码器“ audio / pcm”。

QAudioDecoder decoder;
decoder.setSourceFilename(fileUrl.toLocalFile());
qDebug() << decoder.audioFormat().codec();

How do I know if the audio file's format is supported by Qt media player by not just checking if the file's name ends with ".mp3" or ".wav"? 如何通过不仅仅检查文件名是否以“ .mp3”或“ .wav”结尾的方式来知道Qt媒体播放器是否支持音频文件的格式?

And I found that some files' duration incorrect when playing on my music player. 而且我发现在音乐播放器上播放某些文件时长不正确。 But when I play the file with other music player apps, it gives correct duration on other music players. 但是,当我使用其他音乐播放器应用程序播放文件时,它会在其他音乐播放器上提供正确的持续时间。

Or my app can't play some files but others can. 或我的应用无法播放某些文件,但其他可以。
the console says : 控制台说:

DirectShowPlayerService::doSetUrlSource: Unresolved error code 0x80070002 (?t?Χ??????w?????C) DirectShowPlayerService :: doSetUrlSource:未解决的错误代码0x80070002(?t?Χ?????? w ?????? C)

but after I change the file name, it can be opened. 但更改文件名后,可以将其打开。

How should I fix those on my music player instead of fixing the files' probably wrong or unsupported formats one by one? 我应该如何在音乐播放器上修复这些文件,而不是一一修复文件的可能错误或不受支持的格式?


I wrote this code to check if the file I opened is a supported audio file. 我写了这段代码来检查我打开的文件是否是受支持的音频文件。

QMediaPlayer audioChecker;
audioChecker.setMedia(fileUrl);
qDebug() << audioChecker.media().canonicalUrl().fileName();
qDebug() << audioChecker.isAudioAvailable();
if(audioChecker.error() == QMediaPlayer::NoError) {
    qDebug() << "no error";
}
if(audioChecker.error() == QMediaPlayer::FormatError) {
    qDebug() << "format error";
}

and it tells me no error no matter what file I tested. 无论我测试了哪个文件,它都告诉我没有错误。

console: 安慰:

"musicplayer.exe" “ musicplayer.exe”
false
no error 没错
"musicplayer.exe" “ musicplayer.exe”
false
no error 没错
"musicplayer.ilk" “ musicplayer.ilk”
false
no error 没错
"musicplayer.exe" “ musicplayer.exe”
false
no error 没错
"a song.mp3" “一个song.mp3”
false
no error 没错

But how can files not audio file get no error? 但是,没有音频的文件怎么会没有错误呢? Why do audio file's audio not available? 为什么音频文件的音频不可用?


Never mind. 没关系。 I just read that setMedia() return immediately and doesn't wait for the media to be loaded. 我刚刚读到setMedia()立即返回,并且不等待媒体加载。 so it's normal it gets no audio and no error because it's not loaded. 因此这是正常的,因为没有加载,所以没有音频,也没有错误。 I guess I'll make a thread class to check my file. 我想我将创建一个线程类来检查我的文件。

By the way, incorrect duration of the file is fixed somehow. 顺便说一句,文件的不正确的持续时间是固定的。 Does it relate to LAV Splitter? 它与LAV分离器有关吗?

  1. About how to check audio format in file without check its suffix, there's a tool names mediainfo , with the command line tool run as below: 关于如何在不检查其后缀的情况下检查文件中的音频格式,有一个名为mediainfo的工具,其命令行工具如下运行:

     mediainfo -f [filepath] 

    You'll get the full information about the file, both video and audio if they exists. 您将获得有关文件的完整信息,包括视频和音频(如果存在)。 And among these parameters, there's a "format" value under "Audio" category, that indicates the actual audio format inside. 在这些参数中,“音频”类别下有一个“格式”值,指示内部的实际音频格式。 eg. 例如。 AAC/AC3 AAC / AC3

  2. About the file can be played after name changed, I consider maybe there are some space characters in the filename, or the Player just can't access the file specified by name. 关于文件可以在更改名称后播放,我认为文件名中可能包含一些空格字符,或者播放器无法访问由名称指定的文件。

From Qt's documentation : Qt的文档中

For playing media or audio files that are not simple, uncompressed audio, you can use the QMediaPlayer C++ class […] The compressed audio formats supported does depend on the operating system environment, and also what media plugins the user may have installed. 对于播放不是简单的未压缩音频的媒体或音频文件,可以使用QMediaPlayer C ++类[…]支持的压缩音频格式取决于操作系统环境,以及用户可能安装的媒体插件。

A simple example of how to use the QMediaPlayer class: 一个简单的示例如何使用QMediaPlayer类:

player = new QMediaPlayer;
// ...
player->setMedia(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3"));
player->setVolume(50);
player->play();

To verify that the codec is supported, you can use the error() method of the QMediaPlayer class which will return you an error code (see the documentation ). 要验证是否支持编解码器,可以使用QMediaPlayer类的error()方法,该方法将为您返回错误代码(请参见文档 )。

So, after calling setMedia() , you can do something like: 因此,在调用setMedia() ,您可以执行以下操作:

if (player->error() == QMediaPlayer::FormatError)
{
    // Codec/format unsupported
}

About the error code you get when opening files: 关于打开文件时得到的错误代码:

  • check that you have no \\ characters in your path (if you have some, escape them with another \\ ) 检查路径中是否没有\\字符(如果有的话,请换一个\\
  • check if you have spaces, special characters, etc. 检查您是否有空格,特殊字符等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM