Skip to content

Instantly share code, notes, and snippets.

@adminy
Last active December 12, 2020 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adminy/ff5b40942be2337a36440026c06bf93f to your computer and use it in GitHub Desktop.
Save adminy/ff5b40942be2337a36440026c06bf93f to your computer and use it in GitHub Desktop.
const isNewFFmpegProcess = input => RegExp('^ffmpeg version', 'g').test(input)
const isWarning = input => RegExp('Configuration of video device failed, falling back to default', 'g').test(input)
const isInput = input => RegExp('^Input #', 'g').test(input)
const isStreamMapping = input => RegExp('^Stream mapping', 'g').test(input)
const isOutput = input => RegExp('^Output #', 'g').test(input)
const isFrame = input => RegExp('^frame=', 'g').test(input)
const isSize = input => RegExp('^size=', 'g').test(input)
const match = (expression, input) => RegExp(expression, 'g').exec(input).slice(1).map(x => x.trim())
const sizes = { b: 1, kb: 1024, mb: 1024 * 1024, gb: 1024 * 1024 * 1024 }
const toBytes = size => parseInt(size) * sizes[size.substr(-2).toLowerCase()]
const toBits = bitRate => Math.round(parseFloat(bitRate) * sizes[bitRate.substr(-7, 2)])
const toMS = time => time.split(':').map((t, i) => t * (60 ** (2 - i) )).reduce((a, b) => a + b, 0) * 1000
const extractFrame = input => {
const query = 'frame=(.*?)fps=(.*?)q=(.*?)size=(.*?)time=(.*?)bitrate=(.*?)dup=(.*?)drop=(.*?)speed=(.*?)x'
const [frame, fps, q, size, time, bitRate, dup, drop, speed] = match(query, input)
return {
frame: parseInt(frame),
// fps: parseFloat(fps),
// quality: parseFloat(q),
size: toBytes(size),
time: toMS(time),
bitRate: toBits(bitRate), //bits
// dup: parseInt(dup),
drop: parseInt(drop),
speed: parseFloat(speed)
}
}
const extractSize = input => {
const [size, time, bitRate, speed] = match('size=(.*?)time=(.*?)bitrate=(.*?)speed=(.*?)x', input)
return {
size: toBytes(size), //bytes
time: toMS(time), //ms
bitRate: toBits(bitRate), //bits
speed: parseFloat(speed)
}
}
const lines = [`ffmpeg version 4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
built with Apple clang version 12.0.0 (clang-1200.0.32.27)
configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.1_4 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --disable-libjack --disable-indev=jack
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100`,
`[AVFoundation indev @ 0x7f986ec04640] Configuration of video device failed, falling back to default.`,
`Input #0, avfoundation, from '1:':
Duration: N/A, start: 114417.535333, bitrate: N/A
Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 3840x2160, 30 tbr, 1000k tbn, 1000k tbc`,
`Stream mapping:
Stream #0:0 -> #0:0 (rawvideo (native) -> hevc (hevc_videotoolbox))
Press [q] to stop, [?] for help`,
`Output #0, hevc, to 'pipe:1':
Metadata:
encoder : Lavf58.45.100
Stream #0:0: Video: hevc (hevc_videotoolbox), yuv420p, 1280x720, q=2-31, 1000 kb/s, 30 fps, 30 tbn, 30 tbc
Metadata:
encoder : Lavc58.91.100 hevc_videotoolbox`,
`frame= 18 fps=0.0 q=-0.0 size= 308kB time=00:00:00.43 bitrate=5828.2kbits/s dup=4 drop=0 speed=0.848x `,
`size= 0kB time=00:00:00.48 bitrate= 2.3kbits/s speed=0.967x `
]
for (const line of lines) {
if(isFrame(line)) {
console.log(extractFrame(line))
} else if(isSize(line)) {
console.log(extractSize(line))
} else if(isNewFFmpegProcess(line) || isWarning(line) || isInput(line) || isOutput(line) || isStreamMapping(line)) {
console.log('Loading ...')
} else throw new Error(line)
}
@adminy
Copy link
Author

adminy commented Dec 12, 2020

Similar work here and here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment