Qianjin longitudinal buy phase such as fu, who v. pulse this feeling.

An overview of the

Sometimes we want to use an image as the cover of our video footage, rather than a frame in the footage. Today use FFMPEG to replace video material covers in bulk.

Environmental equipment

Download and install FFMPEG from the official website

To prepare material

Prepare video footage and cover art

Write a program

package cn.merryyou.file;

import java.io.*;

/** * Created by [email protected] on 2020/3/24 **@VERSION1.0 * /
public class ChangeVedioCover {

    public static final String FFMPEG_PATH = "D:/ffmpeg/bin/ffmpeg.exe"; // ffmpeg program lost
    public static final String FILE_PATH = "E: / BaiduNetdiskDownload/testing"; // List of videos that need to be replaced
    public static final String IMAGE_PATH = "E: / BaiduNetdiskDownload/test / 1. PNG"; // The cover photo needs to be replaced
    public static final String COMMAND = "%s -i %s -i %s -map 1 -map 0 -c copy -disposition:0 attached_pic -y %s"; // ffmpeg replaces the cover command

    public static void main(String[] args) throws Exception {
        printPath(new File(FILE_PATH));
    }

    public static void printPath(File file) throws IOException {
        File[] files = file.listFiles();
        for (File a : files) {
            System.out.println(a.getAbsolutePath());
            if (a.getAbsolutePath().endsWith(".mp4")) {
                String newPath = a.getParent() + "/" + a.getName().substring(0, a.getName().lastIndexOf(".")) + "_.mp4"; // Add an underscore to the end of the newly generated file name
                String cmd = String.format(COMMAND, FFMPEG_PATH, a.getAbsolutePath(), IMAGE_PATH, newPath);
                execCmd(cmd);
                a.delete();// Delete the source file
            }
            if(a.isDirectory()) { printPath(a); }}}public static void execCmd(String cmd) {
        ProcessBuilder builder = new ProcessBuilder("cmd.exe"."/C", cmd);
        Process process = null;
        try {
            process = builder.redirectErrorStream(true).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        InputStream in = process.getInputStream();
        outStream(in);
    }

    private static void outStream(InputStream in) {
        // Use a read output stream class to read
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line;
        // Read the output line by line to the console
        try {
            while((line = br.readLine()) ! =null) {
// System.out.println(line);}}catch(IOException e) { e.printStackTrace(); }}}Copy the code

Results the following

Before modifying the cover

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2020/4/7/171529e3ca33ec54~tplv-t2oaga2asx-image.image

After modifying the cover

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2020/4/7/171529e3caa33bb3~tplv-t2oaga2asx-image.image

conclusion

More ffmpeg command reference links