ffmpeg 命令 java可用

这篇博客介绍了FFmpeg在Java环境中的应用,包括视频格式转换、裁剪、音视频合成等操作,提供了详细的命令示例。同时,展示了如何在Java中使用第三方库jave批量获取音频时长,并提供了相关代码实现。内容涵盖多媒体处理和Java编程实践。
摘要由CSDN通过智能技术生成

ffmpeg 命令 java可用

以下命令仅做参考,有的命令测试时改动后忘记了效果,描述不是很清楚,部分命令可能无法运行 ,慎用-c:v h264_qsv,自己试试学的更多

以后遇到了再更新吧

//转编码格式为适配win7
ffmpeg.exe -i 1.mp4 -pix_fmt yuv420p -vcodec libx264 491.ts
//视频带编码转格式测试,不加速就好了
ffmpeg.exe -i 1.mp4 -vcodec libx264 -c:v h264_qsv 3491.ts
//合成ts格式,启用压缩加速,-c:v h264_qsv 慎用
ffmpeg.exe -loglevel quiet -i 3.mp3 -i 1.mp4 -vcodec copy -c:v h264_qsv 31.ts
//视音频合成转ts
ffmpeg.exe -loglevel quiet -i 1.mp4  -i 3.mp3 -vcodec copy  32.ts 
//视频毫秒级裁剪,0.500毫秒要加1秒
ffmpeg.exe -i a9.mp3 -ss 00:00:00.000 -t 00:00:00.500 -c copy q1.mp3
//视频裁剪 毫秒级,误差在于视频关键帧 1.400 实际裁剪出来是0.400毫秒,计算注意加秒
ffmpeg.exe -i 50-13.mp4 -ss 00:00:00.000 -t 00:00:1.400 -c copy qwe1.mp4
//音视频合成使用原视音频自带的编码格式 copy 是关键字
ffmpeg.exe -i 14.mp4 -i 13.mp4 -vcodec copy -y axiba.mp4
//音视频合成并设置视频编码avc音频编码aac - 通用编码格式
ffmpeg.exe -i 3.wav -i mpeg.mp4 -vcodec libx264 -acodec aac 31.mp4
//通过滤镜给视屏添加文字,时间,logo图片可以是gif动图 可以控制位置
ffmpeg.exe -i 1.mp4 -ignore_loop 0 -i logo.png -filter_complex [1:v]scale=20:20[logo1];[0:v][logo1]overlay=10:10,drawtext=fontfile=simhei.ttf: text='显示的文字:这里插入文字':x=10:y=33:fontsize=12:fontcolor=white:shadowy=1,drawtext=fontfile=arial.ttf:x=w-tw-10:y=h-th-10:fontcolor=white:fontsize=12:text='%{localtime" + "\\" + ":%y-%m-%d}' -ss 00:00:00 -t 00:00:00 -vcodec libx264 -acodec aac 123.mp4
//混音 保留原视频声音,注意音频与视频的载入顺序
ffmpeg.exe -stats -i 1.mp3 -i 2.mp4 -filter_complex amix=inputs=2:duration=first:dropout_transition=2 -vcodec libx264 -acodec aac 123.mp4
//分享时边下边播
ffmpeg -i "concat:1.ts|2.ts|3.ts" -c copy -bsf:a aac_adtstoasc -movflags +faststart ts.mp

下面加个java中批量获取音频时长的代码,毫秒级
单个的自己改改
依赖

 <!--音频信息-->
        <dependency>
            <groupId>com.github.dadiyang</groupId>
            <artifactId>jave</artifactId>
            <version>1.0.5</version>
        </dependency>

调用以下类的processStart方法,把list 放入就可运行

package com.ruoyi.common.utils.FfmpegUtil;

import java.io.*;
import java.util.List;

public class ProcessUtils{

    public void processStart(List<String> command) {
        try {
            ProcessBuilder builder = new ProcessBuilder(command);

            Process process = builder.start();

            InputStream errorStream = process.getErrorStream();
            InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
            BufferedReader br = new BufferedReader(inputStreamReader);

            String line = "";
            while ((line = br.readLine()) != null) {
            }

            if (br != null) {
                br.close();
            }
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
            if (errorStream != null) {
                errorStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("开启失败");
        }
    }

    public void delFiles(String lVideoPath,String lAudioPath){
        new File(lAudioPath).delete();
        new File(lVideoPath).delete();
    }

}

 /**
     * 生成filelist文件
     *
     * @param tempVideoPaths
     * @return
     */
    public static String toFile(List<String> tempVideoPaths) {
        //生成fileList文件
        try {
            String only = UUID.randomUUID().toString().replaceAll("-", "");
            File file = new File(FILEPATHTXT + "\\" + only + ".txt");
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter out = new BufferedWriter(fileWriter);
            for (String outPath : tempVideoPaths) {
                out.write("file '" + outPath + "'");
                out.newLine();
            }
            out.flush();
            out.close();
            return FILEPATHTXT + "\\" + only + ".txt";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 /**
     * 计算音频时长
     *
     * @param audioPaths 音频地址列表
     * @return 时长列表
     * @throws EncoderException
     */
    public List<Integer> audioTime(List<String> audioPaths) throws EncoderException {
        //视频地址列表
        List<Integer> audioTimes = new ArrayList<>();
        for (int i = 0; i < audioPaths.size(); i++) {
            File file = new File(audioPaths.get(i));
            //关键代码
            Encoder encoder = new Encoder();
            MultimediaInfo m = encoder.getInfo(file);
            //此处获取音频的duration信息,毫秒
            double ls = m.getDuration();
            //转一下,向上取整,根据项目自己调整
            double durtion = Math.ceil(ls / 1000);
            int time = (int) durtion;
            audioTimes.add(time);
        }
        return audioTimes;
    }
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值