Skip to content

Instantly share code, notes, and snippets.

@jordicenzano
Created November 23, 2018 00:57
Show Gist options
  • Save jordicenzano/32428be9d196bd938f1ec0acb12aeb25 to your computer and use it in GitHub Desktop.
Save jordicenzano/32428be9d196bd938f1ec0acb12aeb25 to your computer and use it in GitHub Desktop.
Analize video and audio duration of all .ts files in the same directory
#!/usr/bin/env node
const fs = require('fs');
const execSync = require('child_process').execSync;
// Local dir ts files
const ts_files = fs.readdirSync('.').filter(fn => fn.endsWith('.ts'));
let n = 0;
const res_total = {video:{ duration_s: 0.0, frames: 0, fr: 0}, audio: { duration_s: 0.0, frames: 0, sr: 0}};
console.log(`Num, Filename, video dur {s}, video frames, audio dur {s}, audio frames, diff (v-a), video frame rate, audio sample rate`)
ts_files.forEach(ts_file => {
const streams_data = get_streams_data(ts_file);
console.log(`${n},${ts_file},${streams_data.video.duration_s},${streams_data.video.frames},${streams_data.audio.duration_s},${streams_data.audio.frames},${streams_data.video.duration_s - streams_data.audio.duration_s},${streams_data.video.fr},${streams_data.audio.sr}`)
res_total.video.duration_s = res_total.video.duration_s + streams_data.video.duration_s;
res_total.audio.duration_s = res_total.audio.duration_s + streams_data.audio.duration_s;
res_total.video.frames = res_total.video.frames + streams_data.video.frames;
res_total.audio.frames = res_total.audio.frames + streams_data.audio.frames;
n = n + 1;
});
console.log('');
console.log(`Totals duration: video:${res_total.video.duration_s}, Audio:${res_total.audio.duration_s}, Diff (video - audio): ${res_total.video.duration_s - res_total.audio.duration_s}`);
console.log(`Totals Frames: video:${res_total.video.frames}, Audio:${res_total.audio.frames}`);
function get_streams_data(file) {
const res = {video:{ duration_s: 0.0, frames: 0, fr: ''}, audio: { duration_s: 0.0, frames: 0, sr: ''}};
const data_str = execSync(`ffprobe -count_frames -show_streams -print_format json ${file}`).toString();
const data_json = JSON.parse(data_str);
if ('streams' in data_json) {
data_json.streams.forEach(stream => {
if (('codec_type' in stream) && (stream.codec_type.toLowerCase() ==='video')) {
if ('duration' in stream) {
res.video.duration_s = parseFloat(stream.duration);
}
if ('nb_read_frames' in stream) {
res.video.frames = parseInt(stream.nb_read_frames);
}
if ('r_frame_rate' in stream) {
res.video.fr = stream.r_frame_rate;
}
}
else if (('codec_type' in stream) && (stream.codec_type.toLowerCase() ==='audio')) {
if ('duration' in stream) {
res.audio.duration_s = parseFloat(stream.duration);
}
if ('nb_read_frames' in stream) {
res.audio.frames = parseInt(stream.nb_read_frames);
}
if ('codec_time_base' in stream) {
res.audio.sr = stream.codec_time_base;
}
}
})
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment