2013-04-11 79 views
1

我想上傳到ffmpeg期間管道視頻實時創建縮略圖。 一切正常,但沒有創建thumbnail.jpg,並且ffmpeg stderr在庫版本顯示後掛起。Node.JS:FFmpeg管道視頻編碼沒有創建縮略圖

更新:我更新了我的代碼,但它也沒有創建一個有效的縮略圖。

var formidable = require('formidable'), 
     http = require('http'), 
     sys = require('sys'), 
     spawn = require('child_process').spawn; 

function spawnFfmpeg(exitCallback) { 
    var args = ['-i', 'pipe:0', '-c:v', 'mjpeg', '-ss', '00:00:13', '-vframes', '1', '-s', '100x80', 'thumbnail.jpg'] 
    var ffmpeg = spawn('ffmpeg', args); 
    console.log('Spawning ffmpeg ' + args.join(' ')); 

    ffmpeg.on('exit', exitCallback); 
    ffmpeg.stderr.on('data', function(data) { 
     console.log('grep stderr: ' + data); 
    }); 
    return ffmpeg; 
} 

http.createServer(function(req, res) { 
    if (req.url == '/' && req.method.toLowerCase() == 'get') { 
     // show a file upload form 
     res.writeHead(200, {'content-type': 'text/html'}); 
     res.end 
       ('<form action="/upload" enctype="multipart/form-data" method="post">' 
         + '<input type="text" name="title"><br>' 
         + '<input type="file" name="upload" multiple="multiple"><br>' 
         + '<input type="submit" value="Upload">' 
         + '</form>' 
         ); 
    } else if (req.url == '/upload' && req.method.toLowerCase() == 'post') { 
     // parse a file upload 
     var form = new formidable.IncomingForm(); 
     form.maxFieldsSize = 29 * 1024 * 1024; 
     // Handle each part of the multi-part post 
     var ffmpeg = spawnFfmpeg(function(code) { 
      console.log('child process exited with code ' + code); 
      res.end(); 
     }); 

     var form = new formidable.IncomingForm(); 
     // Handle each part of the multi-part post 
     form.onPart = function(part) { 
      // Handle each data chunk as data streams in 
      part.addListener('data', function(data) { 
       ffmpeg.stdout.pipe(res); 
       res.pipe(ffmpeg.stdin); 
       // Write each chunk to disk 
       //savedFile.write(data); 
      }); 
     }; 

     // Do it 
     form.parse(req); 
     return; 
    } 
}).listen(80, "127.0.0.1"); 

process.on('uncaughtException', function(err) { 
}); 

回答

2

一些研究和許多小時的測試IVE後找到正確的解決方案^ _^

var formidable = require('formidable'), 
     http = require('http'), 
     sys = require('sys'), 
     spawn = require('child_process').spawn; 

function spawnFfmpeg(exitCallback) { 
    var args = ['-i', 'pipe:0', '-c:v', 'mjpeg', '-ss', '00:00:13', '-vframes', '1', '-s', '100x80', 'thumbnail.jpg'] 
    var ffmpeg = spawn('ffmpeg', args); 
    console.log('Spawning ffmpeg ' + args.join(' ')); 

    ffmpeg.on('exit', exitCallback); 
    ffmpeg.stderr.on('data', function(data) { 
     console.log('grep stderr: ' + data); 
    }); 
    return ffmpeg; 
} 

http.createServer(function(req, res) { 
    if (req.url == '/' && req.method.toLowerCase() == 'get') { 
     // show a file upload form 
     res.writeHead(200, {'content-type': 'text/html'}); 
     res.end 
       ('<form action="/upload" enctype="multipart/form-data" method="post">' 
         + '<input type="text" name="title"><br>' 
         + '<input type="file" name="upload" multiple="multiple"><br>' 
         + '<input type="submit" value="Upload">' 
         + '</form>' 
         ); 
    } else if (req.url == '/upload' && req.method.toLowerCase() == 'post') { 
     // parse a file upload 
     var form = new formidable.IncomingForm(); 
     form.maxFieldsSize = 29 * 1024 * 1024; 
     // Handle each part of the multi-part post 
     var ffmpeg = spawnFfmpeg(function(code) { 
      console.log('child process exited with code ' + code); 
      res.end(); 
     }); 

     var form = new formidable.IncomingForm(); 
     // Handle each part of the multi-part post 
     form.onPart = function(part) { 
      // Handle each data chunk as data streams in 
      part.addListener('data', function(data) { 
       /* 
       * This only one line was the solution of my problem now all works really fast !! 500mbit like transloadit it does 
       */ 
       ffmpeg.stdin.write(data); 
      }); 
     }; 

     // Do it 
     form.parse(req); 
     return; 
    } 
}).listen(80, "127.0.0.1"); 

process.on('uncaughtException', function(err) { 
}); 
0

您的代碼看起來像在管道安裝之前啓動ffmpeg。

以下是未驗證的,但聽起來像你需要:

http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

的「標準輸入輸出」選項child_process.spawn()是其中每個索引對應於FD的數組兒童。值爲以下值之一:

'pipe' - 在子進程和父進程之間創建管道。管道的父端作爲ChildProcess.stdio [fd]作爲child_process對象的屬性公開。爲fds 0 - 2創建的管道也分別以ChildProcess.stdin,ChildProcess.stdout和ChildProcess.stderr的形式提供。

+0

所有測試,但我沒有找到有效的解決方案。 – 2013-04-12 17:57:51

+0

解決方案現在成立於我,謝謝你的大力幫助sascha – 2013-04-12 21:45:02