如何用ffmpeg获得音频和视频的解码器名称?
时间: 2024-11-16 17:28:50 浏览: 23
在FFmpeg中,获取音频和视频的解码器名称通常是在处理媒体文件时完成的,特别是在解析文件头或检测输入内容时。你可以通过调用特定命令行工具或者在代码中使用FFmpeg API来实现。以下是两种常用的方式:
**使用命令行**:
- 对于一个视频文件:
```sh
ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 input.mp4
```
- 对于一个音频文件:
```sh
ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 input.mp3
```
这会分别返回视频和音频的解码器名称。
**使用FFmpeg C API**:
```c
#include <libavformat/avformat.h>
// ...
AVFormatContext *ctx = avformat_open_input(&input, "path_to_your_file", NULL, NULL);
if (!ctx) {
// handle error
}
AVStream *audio_stream = av_find_best_stream(ctx, AVMEDIA_TYPE_AUDIO, 0, 0, NULL, 0);
if (audio_stream) {
const char *audio_codec_name = av_get_codec_name(audio_stream->codec);
printf("Audio codec name: %s\n", audio_codec_name);
AVStream *video_stream = av_find_best_stream(ctx, AVMEDIA_TYPE_VIDEO, 0, 0, NULL, 0);
if (video_stream) {
const char *video_codec_name = av_get_codec_name(video_stream->codec);
printf("Video codec name: %s\n", video_codec_name);
}
avformat_close_input(&ctx);
```
这段代码首先打开并解析文件,然后查找音频和视频流,并获取它们的解码器名称。
阅读全文
相关推荐


















