forked from forks/ffprobe-wasm
Update libx264 build and main.cpp with more fields.
This commit is contained in:
55
src/main.cpp
55
src/main.cpp
@@ -1,4 +1,6 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
#include <emscripten.h>
|
||||
#include <emscripten/bind.h>
|
||||
|
||||
@@ -8,21 +10,28 @@ extern "C" {
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libavformat/avformat.h>
|
||||
#include <libavutil/avutil.h>
|
||||
#include <libavutil/imgutils.h>
|
||||
};
|
||||
|
||||
const std::string c_avformat_version() {
|
||||
return AV_STRINGIFY(LIBAVFORMAT_VERSION);
|
||||
}
|
||||
|
||||
struct Stream {
|
||||
typedef struct Stream {
|
||||
int id;
|
||||
int start_time;
|
||||
int duration;
|
||||
int codec_type;
|
||||
};
|
||||
typedef struct Stream Stream;
|
||||
std::string codec_name;
|
||||
std::string format;
|
||||
int bit_rate;
|
||||
std::string profile;
|
||||
int level;
|
||||
int width;
|
||||
int height;
|
||||
} Stream;
|
||||
|
||||
struct FileInfoResponse {
|
||||
typedef struct FileInfoResponse {
|
||||
std::string name;
|
||||
int bit_rate;
|
||||
int duration;
|
||||
@@ -30,8 +39,7 @@ struct FileInfoResponse {
|
||||
int nb_streams;
|
||||
int flags;
|
||||
std::vector<Stream> streams;
|
||||
};
|
||||
typedef struct FileInfoResponse FileInfoResponse;
|
||||
} FileInfoResponse;
|
||||
|
||||
FileInfoResponse get_file_info() {
|
||||
FILE *file = fopen("file", "rb");
|
||||
@@ -52,6 +60,11 @@ FileInfoResponse get_file_info() {
|
||||
printf("%s", av_err2str(ret));
|
||||
}
|
||||
|
||||
// Get stream info from format.
|
||||
if (avformat_find_stream_info(pFormatContext, NULL) < 0) {
|
||||
printf("ERROR: could not get stream info\n");
|
||||
}
|
||||
|
||||
// Initialize response struct with format data.
|
||||
FileInfoResponse r = {
|
||||
.name = pFormatContext->iformat->name,
|
||||
@@ -71,13 +84,30 @@ FileInfoResponse get_file_info() {
|
||||
for (int i = 0; i < pFormatContext->nb_streams; i++) {
|
||||
AVCodecParameters *pLocalCodecParameters = NULL;
|
||||
pLocalCodecParameters = pFormatContext->streams[i]->codecpar;
|
||||
Stream s = {
|
||||
|
||||
// Convert to char byte array.
|
||||
uint32_t n = pLocalCodecParameters->codec_tag;
|
||||
char fourcc[5];
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
fourcc[j] = (n >> (j * 8) & 0xFF);
|
||||
}
|
||||
fourcc[4] = 0x00; // NULL terminator.
|
||||
|
||||
Stream stream = {
|
||||
.id = (int)pFormatContext->streams[i]->id,
|
||||
.start_time = (int)pFormatContext->streams[i]->start_time,
|
||||
.duration = (int)pFormatContext->streams[i]->duration,
|
||||
.codec_type = (int)pLocalCodecParameters->codec_type
|
||||
.codec_type = (int)pLocalCodecParameters->codec_type,
|
||||
.codec_name = fourcc,
|
||||
.format = av_get_pix_fmt_name((AVPixelFormat)pLocalCodecParameters->format),
|
||||
.bit_rate = (int)pLocalCodecParameters->bit_rate,
|
||||
.profile = avcodec_profile_name(pLocalCodecParameters->codec_id, pLocalCodecParameters->profile),
|
||||
.level = (int)pLocalCodecParameters->level,
|
||||
.width = (int)pLocalCodecParameters->width,
|
||||
.height = (int)pLocalCodecParameters->height,
|
||||
};
|
||||
r.streams.push_back(s);
|
||||
r.streams.push_back(stream);
|
||||
free(fourcc);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
@@ -93,6 +123,13 @@ EMSCRIPTEN_BINDINGS(FileInfoResponse_struct) {
|
||||
.field("start_time", &Stream::start_time)
|
||||
.field("duration", &Stream::duration)
|
||||
.field("codec_type", &Stream::codec_type)
|
||||
.field("codec_name", &Stream::codec_name)
|
||||
.field("format", &Stream::format)
|
||||
.field("bit_rate", &Stream::bit_rate)
|
||||
.field("profile", &Stream::profile)
|
||||
.field("level", &Stream::level)
|
||||
.field("width", &Stream::width)
|
||||
.field("height", &Stream::height)
|
||||
;
|
||||
register_vector<Stream>("Stream");
|
||||
emscripten::value_object<FileInfoResponse>("FileInfoResponse")
|
||||
|
||||
Reference in New Issue
Block a user