Skip to content

Instantly share code, notes, and snippets.

@bongole
Last active October 14, 2020 16:33
Show Gist options
  • Save bongole/b3e97f49e8f4062b070dfc23aeee7281 to your computer and use it in GitHub Desktop.
Save bongole/b3e97f49e8f4062b070dfc23aeee7281 to your computer and use it in GitHub Desktop.
extract raw I420 frame from ffmpeg with node.js
const tmp = require('tmp')
tmp.setGracefulCleanup()
const spawn = require('child_process').spawn
const net = require('net')
async function sleep(t){
return new Promise((res) => setTimeout(res, t))
}
(async function(){
const tmpDir = tmp.dirSync({ prefix: 'mytest' })
const audioSockPath = `${tmpDir.name}/audio.sock`
const audioServer = net.createServer((c) => {
c.on('data', (d) => {
})
})
const videoSockPath = `${tmpDir.name}/video.sock`
const videoServer = net.createServer((c) => {
let fps = 0
const init_size = 1920*1080*1.5
let tmp = init_size
c.on('data', (d) => {
tmp -= d.length
if( tmp < 0 ){
tmp = init_size - Math.abs(tmp)
fps++
}
})
setInterval(() => {
console.log('fps', fps)
fps = 0
}, 1000)
})
audioServer.listen(audioSockPath)
videoServer.listen(videoSockPath)
console.log('server start')
await sleep(1000)
const cmd = spawn('ffmpeg', `-re -i /home/bongole/bigbunny.mp4 -an -f image2pipe -pix_fmt yuv420p -vcodec rawvideo unix://${videoSockPath} -vn -f s16le unix://${audioSockPath}`.split(/\s+/))
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment