Skip to content

Commit 8d63b3a

Browse files
izogfifizotov
authored andcommitted
ffprobe command was added. Missing definitions were added. Code reformat.
1 parent ae1cdac commit 8d63b3a

File tree

8 files changed

+93
-17
lines changed

8 files changed

+93
-17
lines changed

build/ffmpeg-wasm.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ CONF_FLAGS=(
4949
src/fftools/ffmpeg_mux.c
5050
src/fftools/ffmpeg_opt.c
5151
src/fftools/opt_common.c
52+
src/fftools/ffprobe.c
5253
)
5354

5455
emcc "${CONF_FLAGS[@]}" $@

packages/ffmpeg/src/classes.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export class FFmpeg {
6262
case FFMessageType.MOUNT:
6363
case FFMessageType.UNMOUNT:
6464
case FFMessageType.EXEC:
65+
case FFMessageType.FFPROBE:
6566
case FFMessageType.WRITE_FILE:
6667
case FFMessageType.READ_FILE:
6768
case FFMessageType.DELETE_FILE:
@@ -249,6 +250,42 @@ export class FFmpeg {
249250
signal
250251
) as Promise<number>;
251252

253+
/**
254+
* Execute ffprobe command.
255+
*
256+
* @example
257+
* ```ts
258+
* const ffmpeg = new FFmpeg();
259+
* await ffmpeg.load();
260+
* await ffmpeg.writeFile("video.avi", ...);
261+
* // 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
262+
* await ffmpeg.ffprobe(["-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", "video.avi", "-o", "output.txt"]);
263+
* const data = ffmpeg.readFile("output.txt");
264+
* ```
265+
*
266+
* @returns `0` if no error, `!= 0` if timeout (1) or error.
267+
* @category FFmpeg
268+
*/
269+
public ffprobe = (
270+
/** ffprobe command line args */
271+
args: string[],
272+
/**
273+
* milliseconds to wait before stopping the command execution.
274+
*
275+
* @defaultValue -1
276+
*/
277+
timeout = -1,
278+
{ signal }: FFMessageOptions = {}
279+
): Promise<number> =>
280+
this.#send(
281+
{
282+
type: FFMessageType.FFPROBE,
283+
data: { args, timeout },
284+
},
285+
undefined,
286+
signal
287+
) as Promise<number>;
288+
252289
/**
253290
* Terminate all ongoing API calls and terminate web worker.
254291
* `FFmpeg.load()` must be called again before calling any other APIs.

packages/ffmpeg/src/const.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const CORE_URL = `https://unpkg.com/@ffmpeg/core@${CORE_VERSION}/dist/umd
77
export enum FFMessageType {
88
LOAD = "LOAD",
99
EXEC = "EXEC",
10+
FFPROBE = "FFPROBE",
1011
WRITE_FILE = "WRITE_FILE",
1112
READ_FILE = "READ_FILE",
1213
DELETE_FILE = "DELETE_FILE",

packages/ffmpeg/src/worker.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ const exec = ({ args, timeout = -1 }: FFMessageExecData): ExitCode => {
100100
return ret;
101101
};
102102

103+
const ffprobe = ({ args, timeout = -1 }: FFMessageExecData): ExitCode => {
104+
ffmpeg.setTimeout(timeout);
105+
ffmpeg.ffprobe(...args);
106+
const ret = ffmpeg.ret;
107+
ffmpeg.reset();
108+
return ret;
109+
};
110+
103111
const writeFile = ({ path, data }: FFMessageWriteFileData): OK => {
104112
ffmpeg.FS.writeFile(path, data);
105113
return true;
@@ -170,6 +178,9 @@ self.onmessage = async ({
170178
case FFMessageType.EXEC:
171179
data = exec(_data as FFMessageExecData);
172180
break;
181+
case FFMessageType.FFPROBE:
182+
data = ffprobe(_data as FFMessageExecData);
183+
break;
173184
case FFMessageType.WRITE_FILE:
174185
data = writeFile(_data as FFMessageWriteFileData);
175186
break;

packages/types/types/index.d.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,28 @@ export interface Stat {
3939
blocks: number;
4040
}
4141

42-
export interface FSFilesystemWORKERFS {
43-
44-
}
42+
export interface FSFilesystemWORKERFS {}
4543

46-
export interface FSFilesystemMEMFS {
47-
48-
}
44+
export interface FSFilesystemMEMFS {}
4945

5046
export interface FSFilesystems {
5147
WORKERFS: FSFilesystemWORKERFS;
5248
MEMFS: FSFilesystemMEMFS;
5349
}
5450

55-
export type FSFilesystem =
56-
| FSFilesystemWORKERFS
57-
| FSFilesystemMEMFS;
51+
export type FSFilesystem = FSFilesystemWORKERFS | FSFilesystemMEMFS;
52+
53+
export interface OptionReadFile {
54+
encoding: string;
55+
}
56+
57+
export interface WorkerFSMountConfig {
58+
blobs?: {
59+
name: string;
60+
data: Blob;
61+
}[];
62+
files?: File[];
63+
}
5864

5965
/**
6066
* Functions to interact with Emscripten FS library.
@@ -75,7 +81,11 @@ export interface FS {
7581
isFile: (mode: number) => boolean;
7682
/** mode is a numeric notation of permission, @see [Numeric Notation](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation) */
7783
isDir: (mode: number) => boolean;
78-
mount: (fileSystemType: FSFilesystem, data: WorkerFSMountConfig, path: string) => void;
84+
mount: (
85+
fileSystemType: FSFilesystem,
86+
data: WorkerFSMountConfig,
87+
path: string
88+
) => void;
7989
unmount: (path: string) => void;
8090
filesystems: FSFilesystems;
8191
}
@@ -115,6 +125,7 @@ export interface FFmpegCoreModule {
115125
mainScriptUrlOrBlob: string;
116126

117127
exec: (...args: string[]) => number;
128+
ffprobe: (...args: string[]) => number;
118129
reset: () => void;
119130
setLogger: (logger: (log: Log) => void) => void;
120131
setTimeout: (timeout: number) => void;

src/bind/ffmpeg/bind.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
const NULL = 0;
66
const SIZE_I32 = Uint32Array.BYTES_PER_ELEMENT;
77
const DEFAULT_ARGS = ["./ffmpeg", "-nostdin", "-y"];
8+
const DEFAULT_ARGS_FFPROBE = ["./ffprobe"];
89

910
Module["NULL"] = NULL;
1011
Module["SIZE_I32"] = SIZE_I32;
1112
Module["DEFAULT_ARGS"] = DEFAULT_ARGS;
13+
Module["DEFAULT_ARGS_FFPROBE"] = DEFAULT_ARGS_FFPROBE;
1214

1315
/**
1416
* Variables
@@ -62,6 +64,18 @@ function exec(..._args) {
6264
return Module["ret"];
6365
}
6466

67+
function ffprobe(..._args) {
68+
const args = [...Module["DEFAULT_ARGS_FFPROBE"], ..._args];
69+
try {
70+
Module["_ffprobe"](args.length, stringsToPtr(args));
71+
} catch (e) {
72+
if (!e.message.startsWith("Aborted")) {
73+
throw e;
74+
}
75+
}
76+
return Module["ret"];
77+
}
78+
6579
function setLogger(logger) {
6680
Module["logger"] = logger;
6781
}
@@ -121,6 +135,7 @@ Module["printErr"] = printErr;
121135
Module["locateFile"] = _locateFile;
122136

123137
Module["exec"] = exec;
138+
Module["ffprobe"] = ffprobe;
124139
Module["setLogger"] = setLogger;
125140
Module["setTimeout"] = setTimeout;
126141
Module["setProgress"] = setProgress;

src/bind/ffmpeg/export.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
const EXPORTED_FUNCTIONS = ["_ffmpeg", "_abort", "_malloc"];
1+
const EXPORTED_FUNCTIONS = ["_ffmpeg", "_abort", "_malloc", "_ffprobe"];
22

33
console.log(EXPORTED_FUNCTIONS.join(","));

src/fftools/ffprobe.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ typedef struct InputFile {
9090
int nb_streams;
9191
} InputFile;
9292

93-
const char program_name[] = "ffprobe";
94-
const int program_birth_year = 2007;
93+
const char program_name_ffprobe[] = "ffprobe";
94+
const int program_birth_year_ffprobe = 2007;
9595

9696
static int do_bitexact = 0;
9797
static int do_count_frames = 0;
@@ -3491,7 +3491,7 @@ static int probe_file(WriterContext *wctx, const char *filename,
34913491
static void show_usage(void)
34923492
{
34933493
av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
3494-
av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] INPUT_FILE\n", program_name);
3494+
av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] INPUT_FILE\n", program_name_ffprobe);
34953495
av_log(NULL, AV_LOG_INFO, "\n");
34963496
}
34973497

@@ -3503,7 +3503,7 @@ static void ffprobe_show_program_version(WriterContext *w)
35033503
writer_print_section_header(w, SECTION_ID_PROGRAM_VERSION);
35043504
print_str("version", FFMPEG_VERSION);
35053505
print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
3506-
program_birth_year, CONFIG_THIS_YEAR);
3506+
program_birth_year_ffprobe, CONFIG_THIS_YEAR);
35073507
print_str("compiler_ident", CC_IDENT);
35083508
print_str("configuration", FFMPEG_CONFIGURATION);
35093509
writer_print_section_footer(w);
@@ -3740,7 +3740,7 @@ static int opt_print_filename(void *optctx, const char *opt, const char *arg)
37403740
return 0;
37413741
}
37423742

3743-
void show_help_default(const char *opt, const char *arg)
3743+
void show_help_default_ffprobe(const char *opt, const char *arg)
37443744
{
37453745
av_log_set_callback(log_callback_help);
37463746
show_usage();
@@ -4142,7 +4142,7 @@ int ffprobe(int argc, char **argv)
41424142
(!do_show_program_version && !do_show_library_versions && !do_show_pixel_formats))) {
41434143
show_usage();
41444144
av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
4145-
av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
4145+
av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name_ffprobe);
41464146
ret = AVERROR(EINVAL);
41474147
} else if (input_filename) {
41484148
ret = probe_file(wctx, input_filename, print_input_filename);

0 commit comments

Comments
 (0)