9322

Generate video with ffmpeg for JavaFX MediaPlayer

Question:

I'm trying to use ffmpeg for generating a video file from timelapse images. Somehow I didn't find out which codec resp. which parameters I have to use in ffmpeg, that the video file is playable with JavaFX's MediaPlayer. I tried these codecs:

<ul><li><strong>libx264</strong> (ffmpeg -f image2 -i %05d.jpg -r 30 -filter:v crop=4000:2250:0:0 -s 1920x1080 -vcodec libx264 -preset ultrafast -qp 0 Timelapse3.mp4) </li> <li><strong>mpeg4</strong> (ffmpeg -f image2 -i %05d.jpg -r 30 -filter:v crop=4000:2250:0:0 -s 1920x1080 -vcodec mpeg4 -qscale 1 ../Timelapse.avi)</li> </ul>

But they didn't work with JavaFX. What codec and parameter do I have to use for a high quality output?

Answer1:

The following worked for me if I have an input as mp4 (but it should work accordingly with other input formats, as in your case the image timelapse):

ffmpeg -i input.mp4 -vcodec h264 -vf scale=1920x1080 -an -pix_fmt yuv420p output.mp4

So the important parts are:

<ul><li><strong>changing the pix_fmt</strong>. Apparently the default one is not supported.</li> <li>and the resolution seems to be only working if it is <strong>lower than or equal to 1920x1080</strong></li> <li>I don't need audio in my case! (if you need audio <strong>remove the -an</strong> or replace with working audio codec conversion)</li> </ul>

Answer2:

The avi container format isn't supported by JavaFX so won't work - your first example should however play ball ok - I've tried it and it works for me.

You could also try forcing the mp4 container with the same f switch but just before the output file:

ffmpeg -f image2 -i %05d.jpg -r 30 -filter:v crop=4000:2250:0:0 -s 1920x1080 -vcodec mpeg4 -qscale 1 -f mp4 ../Timelapse.mp4

(Also try the above with libx264.)

Answer3:

Try this

Replace the file name in the quotes with your file name. The file name numbering should start with 0 then go to 1, 2, 3, 4 and so on

This is how my file names look

Mandibular hollow 1 micron.gizmofill0.gizmoslice.jpg

Mandibular hollow 1 micron.gizmofill17.gizmoslice.jpg

Mandibular hollow 1 micron.gizmofill16994.gizmoslice.jpg

My files range between 198kb and 47kb in size. I have about 18500 files. All the files combined are about 2.9GB. This will generate a movie file at 50 frames per second that is around 25MB

ffmpeg -framerate 50 -i "Mandibular hollow 1 micron.gizmofill%d.gizmoslice.jpg" -s 1638x1004 -c:v libx264 -pix_fmt yuv420p output.mp4

This controller code works for me

package javafxapplication13; import java.io.File; import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; import javafx.scene.media.MediaView; /** * * @author kobus */ public class FXMLDocumentController implements Initializable { @FXML private MediaView mediaView; @FXML private void handleButtonAction(ActionEvent event) { final File f = new File("C:/Users/kobus/Dropbox/JavaProjects/Gizmetor/temp/output.mp4"); Media media = new Media(f.toURI().toString()); MediaPlayer mediaPlayer = new MediaPlayer(media); mediaPlayer.setAutoPlay(true); mediaPlayer.play(); mediaView.setMediaPlayer(mediaPlayer); } @Override public void initialize(URL url, ResourceBundle rb) { // TODO } }

This is the .fxml file

<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.media.*?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication13.FXMLDocumentController"> <children> <Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" /> <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" /> <MediaView fx:id="mediaView" fitHeight="1005.0" fitWidth="1638.0" layoutX="14.0" layoutY="165.0" /> </children> </AnchorPane>

Here is the application file

package javafxapplication13; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; /** * * @author kobus */ public class JavaFXApplication13 extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }

Recommend

  • Generate video with ffmpeg for JavaFX MediaPlayer
  • how to make triangle using python 2.7 [closed]
  • I need to delete string from position X to position Y on each line in a text file
  • How to do bind two dataframe columns in sparkR?
  • Is it possible to use Microsoft.Office.Interop.Word to programatically remove line numbering from a
  • How to display indirectly given unicode character in Swift?
  • Webpack 2 loading, exposing, and bundling jquery and bootstrap
  • Alternatives to Coldfusion Report Builder
  • Jquery hide first 12 elementes, show next 12 elements
  • Convert a list with non-fixed length elements to tensor
  • iOS Custom Emoticons
  • Converting a Brownfield PHP Webapp to Zend Framework
  • How to properly trigger lazy load of collection in Entity Framework 5?
  • Avoiding try/catch hell in my web pages
  • Access VBScript variable within Javascript inside of an HTA
  • How do I control the soft menu button in Honeycomb?
  • How to convert list into string with quotes in python
  • Grails Transactions and the Session
  • How to prepare a C++ string for sql query
  • How to reshape a 3D numpy array?
  • LNK1104: cannot open file 'kernel32.lib'
  • if some function is not optimized does it mean that all functions where it is declared are not optim
  • Hide HTML elements without javascript, only CSS
  • Copy to all folders batch file?
  • Moving mysql files across servers
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • python regex in pyparsing
  • Android Google Maps API OnLocationChanged only called once
  • How can I use threading to 'tick' a timer to be accessed by other threads?