Skip to content

Commit

Permalink
Added try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Nov 24, 2022
1 parent 57bece4 commit 8d5e2a7
Showing 1 changed file with 75 additions and 60 deletions.
135 changes: 75 additions & 60 deletions src/FFProbe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,78 +5,93 @@ import TempFileService from "./TempFileService";
export default class FFProbe {

public static async probe(url: string, file?: string) {

file ??= await TempFileService.downloadTo(url);

const metadata = await new Promise<any>((resolve, reject) => {
FFConfig.ffmpeg.ffprobe(file, (error, metadata) => {
if (error) {
reject(error);
return;
}
resolve(metadata);
try {

file ??= await TempFileService.downloadTo(url);

const metadata = await new Promise<any>((resolve, reject) => {
FFConfig.ffmpeg.ffprobe(file, (error, metadata) => {
if (error) {
reject(error);
return;
}
resolve(metadata);
});
});
});

// probe can stream ...
// probe is iOS and Android ready ...
// check for MP4 and AAC
// check for fast start
// probe can stream ...
// probe is iOS and Android ready ...
// check for MP4 and AAC
// check for fast start

let indexOfMoov = -1;
let indexOfMDat = -1;
let indexOfMoov = -1;
let indexOfMDat = -1;

const checkPosition = (data, length) => {
let i = data.indexOf("type:'moov'");
if (i !== -1) {
indexOfMoov = length + i;
}
i = data.indexOf("type:'mdat'");
if (i !== -1) {
indexOfMDat = length + i;
}
return indexOfMoov === -1 || indexOfMDat === -1;
};
const checkPosition = (data, length) => {
let i = data.indexOf("type:'moov'");
if (i !== -1) {
indexOfMoov = length + i;
}
i = data.indexOf("type:'mdat'");
if (i !== -1) {
indexOfMDat = length + i;
}
return indexOfMoov === -1 || indexOfMDat === -1;
};

const text = await FFConfig.run(
["-v", "trace",
"-i", file,
"-f", "null", "-"], null, checkPosition);
const text = await FFConfig.run(
["-v", "trace",
"-i", file,
"-f", "null", "-"], null, checkPosition);

const fastStart = indexOfMoov < indexOfMDat;
const fastStart = indexOfMoov < indexOfMDat;

const isAAC = metadata.streams.some((x) => x.codec_type === "audio")
? metadata.streams.some((x) => x.codec_name === "aac" )
: true;
const isAAC = metadata.streams.some((x) => x.codec_type === "audio")
? metadata.streams.some((x) => x.codec_name === "aac" )
: true;

const videoStream = metadata.streams.find((x) => x.codec_type === "video");
const isH264 = !videoStream || (videoStream.codec_name === "h264" );
const videoStream = metadata.streams.find((x) => x.codec_type === "video");
const isH264 = !videoStream || (videoStream.codec_name === "h264" );

let isBelow30FPS = false;
const avgFrameRate = videoStream?.avg_frame_rate;
if (avgFrameRate) {
try {
isBelow30FPS = eval(avgFrameRate) < 31;
} catch (e) {
let isBelow30FPS = false;
const avgFrameRate = videoStream?.avg_frame_rate;
if (avgFrameRate) {
try {
isBelow30FPS = eval(avgFrameRate) < 31;
} catch (e) {

}
}
}

const isMobileReady = isAAC && isH264 && fastStart && isBelow30FPS;

const needsFastStart = isAAC && isH264 && isBelow30FPS && !fastStart;

return {
... metadata,
isAAC,
isH264,
fastStart,
isBelow30FPS,
isMobileReady,
indexOfMoov,
indexOfMDat,
needsFastStart
};
const isMobileReady = isAAC && isH264 && fastStart && isBelow30FPS;

const needsFastStart = isAAC && isH264 && isBelow30FPS && !fastStart;

return {
... metadata,
isAAC,
isH264,
fastStart,
isBelow30FPS,
isMobileReady,
indexOfMoov,
indexOfMDat,
needsFastStart
};
} catch (e) {
console.error(e);
return {
isAAC: false,
isH264: false,
fastStart: false,
isBelow30FPS: false,
isMobileReady: false,
indexOfMoov: 0,
indexOfMDat: 0,
needsFastStart: false,
error: e
};
}
}

}

0 comments on commit 8d5e2a7

Please sign in to comment.