Java through FFmpeg rough processing video transcoding, cover image

This is the 11th day of my participation in Gwen Challenge

Business scenarios:

Transcoding the uploaded video through Java, capturing the cover image, and then uploading the video to the file storage server

The use of tools

  • Download ffMPEG (video processing tool) at ffmpeg.org/download.ht…

  • Into jave. Jar jar package) (tools download address: www.sauronsoftware.it/projects/ja…

(Jave package is an open source framework for dealing with FFMPEG, which is relatively old and lacking in functions. This article only uses it to do a video format conversion function.)

Pom. The XML configuration

<! FFmpeg integrated JAR package -->
<dependency>   
  <groupId>joinery</groupId>   
  <artifactId>jave</artifactId>   
  <version>1.0.2</version> 
</dependency>        
Copy the code

Realize the principle of

Then call the locally installed video processing tool FFmPEG for processing, and place the processed files in local test2. Call the relevant API to upload the files in test2 to the file storage server. After the upload, delete the files in local test1 and Test2

Code implementation

The video transcoding part is directly processed by the method encapsulated in the Jave package, while the video cover image function is not well encapsulated in Jave. Here, the command for processing screenshots is directly assembled in the code, and then ffMPEG can execute this command by calling the command line of the system

Video processing methods

private static void dealStreamCommon(BufferedReader buff) {
         String line = null;
         try {
             while((line = buff.readLine()) ! =null) {
                 logger.warn("output: "+ line); }}catch (IOException e) {
             e.printStackTrace();
         } finally {
             try {
                 buff.close();
             } catch(IOException e) { e.printStackTrace(); }}}Copy the code
 /** * delete file **@param file
      */
     public static void deleteFile(File file) {
         if(file.exists()) { file.delete(); }}Copy the code

Open the thread processing Ffmpeg processing stream

/ * * * *@param process
      */
     private static void dealStream(Process process) {
         if (process == null) {
             return;
         }
         // Process the thread of InputStream
         new Thread() {
             @Override
             public void run(a) {
                 BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
                 dealStreamCommon(in);
             }
         }.start();
         // The thread handling the ErrorStream
         new Thread() {
             @Override
             public void run(a) {
                 BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                 dealStreamCommon(err);
             }
         }.start();
     }
Copy the code

Take a frame from the video as the cover image

    / * * * * *@param fileName
     *            ffmpeg -i /Users/jinx/Downloads/test.mp4 -y -f image2 -ss
     *            00:00:01 -vframes 1 /Users/jinx/Downloads/test.jpeg
     * @return* /
    public static boolean covPic(String fileName, String time, String outName, String size) {
        List<String> command = new ArrayList<String>();
        command.add(FFMPEG_PATH);
        command.add("-i");
        // File input source path
        command.add(fileName);
        command.add("-y");
        command.add("-f");
        command.add("image2");
        command.add("-ss");
        // The captured point in time is in the format of 00:00:01
        command.add(time);
        command.add("-vframes");
        command.add("1");
        command.add("-s");
        // The size of the captured image is 1920x1080
        command.add(size);
        // Output path of the file in the format of 11.jpg
        command.add(outName);

        try {
            Process videoProcess = new ProcessBuilder(command).start();
            dealStream(videoProcess);
            videoProcess.waitFor();
            return true;
        } catch (Exception e) {
        }
        return false;
    }

Copy the code