ffprobe command was added. Missing definitions were added. Code reformat.

This commit is contained in:
izogfif 2024-08-29 06:06:13 +00:00 committed by izotov
parent ae1cdac7db
commit 8d63b3a9c2
8 changed files with 93 additions and 17 deletions

View File

@ -49,6 +49,7 @@ CONF_FLAGS=(
src/fftools/ffmpeg_mux.c src/fftools/ffmpeg_mux.c
src/fftools/ffmpeg_opt.c src/fftools/ffmpeg_opt.c
src/fftools/opt_common.c src/fftools/opt_common.c
src/fftools/ffprobe.c
) )
emcc "${CONF_FLAGS[@]}" $@ emcc "${CONF_FLAGS[@]}" $@

View File

@ -62,6 +62,7 @@ export class FFmpeg {
case FFMessageType.MOUNT: case FFMessageType.MOUNT:
case FFMessageType.UNMOUNT: case FFMessageType.UNMOUNT:
case FFMessageType.EXEC: case FFMessageType.EXEC:
case FFMessageType.FFPROBE:
case FFMessageType.WRITE_FILE: case FFMessageType.WRITE_FILE:
case FFMessageType.READ_FILE: case FFMessageType.READ_FILE:
case FFMessageType.DELETE_FILE: case FFMessageType.DELETE_FILE:
@ -249,6 +250,42 @@ export class FFmpeg {
signal signal
) as Promise<number>; ) as Promise<number>;
/**
* Execute ffprobe command.
*
* @example
* ```ts
* const ffmpeg = new FFmpeg();
* await ffmpeg.load();
* await ffmpeg.writeFile("video.avi", ...);
* // Getting duration of a video in seconds: ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 video.avi -o output.txt
* await ffmpeg.ffprobe(["-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", "video.avi", "-o", "output.txt"]);
* const data = ffmpeg.readFile("output.txt");
* ```
*
* @returns `0` if no error, `!= 0` if timeout (1) or error.
* @category FFmpeg
*/
public ffprobe = (
/** ffprobe command line args */
args: string[],
/**
* milliseconds to wait before stopping the command execution.
*
* @defaultValue -1
*/
timeout = -1,
{ signal }: FFMessageOptions = {}
): Promise<number> =>
this.#send(
{
type: FFMessageType.FFPROBE,
data: { args, timeout },
},
undefined,
signal
) as Promise<number>;
/** /**
* Terminate all ongoing API calls and terminate web worker. * Terminate all ongoing API calls and terminate web worker.
* `FFmpeg.load()` must be called again before calling any other APIs. * `FFmpeg.load()` must be called again before calling any other APIs.

View File

@ -7,6 +7,7 @@ export const CORE_URL = `https://unpkg.com/@ffmpeg/core@${CORE_VERSION}/dist/umd
export enum FFMessageType { export enum FFMessageType {
LOAD = "LOAD", LOAD = "LOAD",
EXEC = "EXEC", EXEC = "EXEC",
FFPROBE = "FFPROBE",
WRITE_FILE = "WRITE_FILE", WRITE_FILE = "WRITE_FILE",
READ_FILE = "READ_FILE", READ_FILE = "READ_FILE",
DELETE_FILE = "DELETE_FILE", DELETE_FILE = "DELETE_FILE",

View File

@ -100,6 +100,14 @@ const exec = ({ args, timeout = -1 }: FFMessageExecData): ExitCode => {
return ret; return ret;
}; };
const ffprobe = ({ args, timeout = -1 }: FFMessageExecData): ExitCode => {
ffmpeg.setTimeout(timeout);
ffmpeg.ffprobe(...args);
const ret = ffmpeg.ret;
ffmpeg.reset();
return ret;
};
const writeFile = ({ path, data }: FFMessageWriteFileData): OK => { const writeFile = ({ path, data }: FFMessageWriteFileData): OK => {
ffmpeg.FS.writeFile(path, data); ffmpeg.FS.writeFile(path, data);
return true; return true;
@ -170,6 +178,9 @@ self.onmessage = async ({
case FFMessageType.EXEC: case FFMessageType.EXEC:
data = exec(_data as FFMessageExecData); data = exec(_data as FFMessageExecData);
break; break;
case FFMessageType.FFPROBE:
data = ffprobe(_data as FFMessageExecData);
break;
case FFMessageType.WRITE_FILE: case FFMessageType.WRITE_FILE:
data = writeFile(_data as FFMessageWriteFileData); data = writeFile(_data as FFMessageWriteFileData);
break; break;

View File

@ -39,22 +39,28 @@ export interface Stat {
blocks: number; blocks: number;
} }
export interface FSFilesystemWORKERFS { export interface FSFilesystemWORKERFS {}
}
export interface FSFilesystemMEMFS { export interface FSFilesystemMEMFS {}
}
export interface FSFilesystems { export interface FSFilesystems {
WORKERFS: FSFilesystemWORKERFS; WORKERFS: FSFilesystemWORKERFS;
MEMFS: FSFilesystemMEMFS; MEMFS: FSFilesystemMEMFS;
} }
export type FSFilesystem = export type FSFilesystem = FSFilesystemWORKERFS | FSFilesystemMEMFS;
| FSFilesystemWORKERFS
| FSFilesystemMEMFS; export interface OptionReadFile {
encoding: string;
}
export interface WorkerFSMountConfig {
blobs?: {
name: string;
data: Blob;
}[];
files?: File[];
}
/** /**
* Functions to interact with Emscripten FS library. * Functions to interact with Emscripten FS library.
@ -75,7 +81,11 @@ export interface FS {
isFile: (mode: number) => boolean; isFile: (mode: number) => boolean;
/** mode is a numeric notation of permission, @see [Numeric Notation](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation) */ /** mode is a numeric notation of permission, @see [Numeric Notation](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation) */
isDir: (mode: number) => boolean; isDir: (mode: number) => boolean;
mount: (fileSystemType: FSFilesystem, data: WorkerFSMountConfig, path: string) => void; mount: (
fileSystemType: FSFilesystem,
data: WorkerFSMountConfig,
path: string
) => void;
unmount: (path: string) => void; unmount: (path: string) => void;
filesystems: FSFilesystems; filesystems: FSFilesystems;
} }
@ -115,6 +125,7 @@ export interface FFmpegCoreModule {
mainScriptUrlOrBlob: string; mainScriptUrlOrBlob: string;
exec: (...args: string[]) => number; exec: (...args: string[]) => number;
ffprobe: (...args: string[]) => number;
reset: () => void; reset: () => void;
setLogger: (logger: (log: Log) => void) => void; setLogger: (logger: (log: Log) => void) => void;
setTimeout: (timeout: number) => void; setTimeout: (timeout: number) => void;

View File

@ -5,10 +5,12 @@
const NULL = 0; const NULL = 0;
const SIZE_I32 = Uint32Array.BYTES_PER_ELEMENT; const SIZE_I32 = Uint32Array.BYTES_PER_ELEMENT;
const DEFAULT_ARGS = ["./ffmpeg", "-nostdin", "-y"]; const DEFAULT_ARGS = ["./ffmpeg", "-nostdin", "-y"];
const DEFAULT_ARGS_FFPROBE = ["./ffprobe"];
Module["NULL"] = NULL; Module["NULL"] = NULL;
Module["SIZE_I32"] = SIZE_I32; Module["SIZE_I32"] = SIZE_I32;
Module["DEFAULT_ARGS"] = DEFAULT_ARGS; Module["DEFAULT_ARGS"] = DEFAULT_ARGS;
Module["DEFAULT_ARGS_FFPROBE"] = DEFAULT_ARGS_FFPROBE;
/** /**
* Variables * Variables
@ -62,6 +64,18 @@ function exec(..._args) {
return Module["ret"]; return Module["ret"];
} }
function ffprobe(..._args) {
const args = [...Module["DEFAULT_ARGS_FFPROBE"], ..._args];
try {
Module["_ffprobe"](args.length, stringsToPtr(args));
} catch (e) {
if (!e.message.startsWith("Aborted")) {
throw e;
}
}
return Module["ret"];
}
function setLogger(logger) { function setLogger(logger) {
Module["logger"] = logger; Module["logger"] = logger;
} }
@ -121,6 +135,7 @@ Module["printErr"] = printErr;
Module["locateFile"] = _locateFile; Module["locateFile"] = _locateFile;
Module["exec"] = exec; Module["exec"] = exec;
Module["ffprobe"] = ffprobe;
Module["setLogger"] = setLogger; Module["setLogger"] = setLogger;
Module["setTimeout"] = setTimeout; Module["setTimeout"] = setTimeout;
Module["setProgress"] = setProgress; Module["setProgress"] = setProgress;

View File

@ -1,3 +1,3 @@
const EXPORTED_FUNCTIONS = ["_ffmpeg", "_abort", "_malloc"]; const EXPORTED_FUNCTIONS = ["_ffmpeg", "_abort", "_malloc", "_ffprobe"];
console.log(EXPORTED_FUNCTIONS.join(",")); console.log(EXPORTED_FUNCTIONS.join(","));

View File

@ -90,8 +90,8 @@ typedef struct InputFile {
int nb_streams; int nb_streams;
} InputFile; } InputFile;
const char program_name[] = "ffprobe"; const char program_name_ffprobe[] = "ffprobe";
const int program_birth_year = 2007; const int program_birth_year_ffprobe = 2007;
static int do_bitexact = 0; static int do_bitexact = 0;
static int do_count_frames = 0; static int do_count_frames = 0;
@ -3491,7 +3491,7 @@ end:
static void show_usage(void) static void show_usage(void)
{ {
av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n"); av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] INPUT_FILE\n", program_name); av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] INPUT_FILE\n", program_name_ffprobe);
av_log(NULL, AV_LOG_INFO, "\n"); av_log(NULL, AV_LOG_INFO, "\n");
} }
@ -3503,7 +3503,7 @@ static void ffprobe_show_program_version(WriterContext *w)
writer_print_section_header(w, SECTION_ID_PROGRAM_VERSION); writer_print_section_header(w, SECTION_ID_PROGRAM_VERSION);
print_str("version", FFMPEG_VERSION); print_str("version", FFMPEG_VERSION);
print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers", print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
program_birth_year, CONFIG_THIS_YEAR); program_birth_year_ffprobe, CONFIG_THIS_YEAR);
print_str("compiler_ident", CC_IDENT); print_str("compiler_ident", CC_IDENT);
print_str("configuration", FFMPEG_CONFIGURATION); print_str("configuration", FFMPEG_CONFIGURATION);
writer_print_section_footer(w); writer_print_section_footer(w);
@ -3740,7 +3740,7 @@ static int opt_print_filename(void *optctx, const char *opt, const char *arg)
return 0; return 0;
} }
void show_help_default(const char *opt, const char *arg) void show_help_default_ffprobe(const char *opt, const char *arg)
{ {
av_log_set_callback(log_callback_help); av_log_set_callback(log_callback_help);
show_usage(); show_usage();
@ -4142,7 +4142,7 @@ int ffprobe(int argc, char **argv)
(!do_show_program_version && !do_show_library_versions && !do_show_pixel_formats))) { (!do_show_program_version && !do_show_library_versions && !do_show_pixel_formats))) {
show_usage(); show_usage();
av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n"); av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name); av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name_ffprobe);
ret = AVERROR(EINVAL); ret = AVERROR(EINVAL);
} else if (input_filename) { } else if (input_filename) {
ret = probe_file(wctx, input_filename, print_input_filename); ret = probe_file(wctx, input_filename, print_input_filename);