[英]Java - Write Video and Audio at the same time to FFmpeg
你好開發者,
我目前正在開發一種工具,可以通過使用Java Process執行FFmpeg並向其提供視頻幀來呈現視頻。
我目前使用以下FFmpeg命令: ffmpeg -y -f rawvideo -pix_fmt rgb24 -s %WIDTH%x%HEIGHT% -r %FPS% -i - -an -c:v libx264 -preset ultrafast -pix_fmt yuv420p "%FILENAME%.mp4"
,其中占位符顯然被實際值替換。
我用來初始化FFmpeg的代碼:
//commandArgs is a list of command line arguments for FFmpeg
List<String> command = new ArrayList<String>();
command.add("ffmpeg");
command.addAll(commandArgs);
process = new ProcessBuilder(command).directory(outputFolder).start();
OutputStream exportLogOut = new FileOutputStream("export.log");
new StreamPipe(process.getInputStream(), exportLogOut).start();
new StreamPipe(process.getErrorStream(), exportLogOut).start();
outputStream = process.getOutputStream();
channel = Channels.newChannel(outputStream);
然后,我有以下方法將包含視頻幀的ByteBuffer寫入FFmpeg:
public void consume(ByteBuffer buf) {
try {
channel.write(buf);
ByteBufferPool.release(buf);
} catch(Exception e) {
e.printStackTrace();
}
}
現在,我的問題是,我將如何繼續將同步音頻數據寫入輸出文件? 我假設我需要使用多個管道,當然我將不得不修改我的命令行參數,但我需要幫助:
1) what kind of Audio Data do I need to feed FFmpeg with?
2) how do I feed Audio and Video in one go?
3) how do I keep Audio and Video synchronized?
在此先感謝您的幫助!
問候,CrushedPixel
看一下https://github.com/artclarke/humble-video ,它是java中ffmpeg的包裝器。 您可以將視頻/音頻流以編碼方式添加到編碼器中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.