Capture Thumbnail Whilte Downloading Youtube Video

2019-07-18 03:04发布

问题:

I want to capture a thumbnail of Youtube video on certain timeline. (e.g. 3.2sec) I used ytdl and fluent-ffmpeg with node.js to implement it. It was possible to capture a thumbnail when the downloading has finished.

var fs = require('fs');
var ytdl = require('ytdl');
var ffmpeg = require('fluent-ffmpeg');
var videostream = fs.createWriteStream('video.flv');
var myytdl = ytdl('http://www.youtube.com/watch?v=A02s8omM_hI');
myytdl.pipe(videostream);
videostream.on('close',function() {
  console.log('Downloading complete...');
  var proc = new ffmpeg({ source: 'video.flv', nolog: true })
  .withSize('150x100')
  .takeScreenshots(1, '/home/test', function(err) {
    console.log(err || 'Screenshots were saved');
  });
});

However, I couldn't implement to capture a thumbnail while downloading. Basic idea of what I want to do is as below.

  1. Download Youtube video starting at X sec. (Worked)
  2. Pipe it to Readable/Writable(Duplex) Memory Stream (Need Advise)
  3. During downloading, check if stream has enough data to capture the first frame, which is at X sec. (Need Advise)
  4. Capture the first frame, then stop downloading

For 2, I've realized that node.js will have Duplex Stream on coming v0.9.x, but it seems not working properly. Anyone who has good idea to implement bullet 2,3?