ffmpeg安装和基本使用,java工具类,amr文件转MP3

java中媒体文件的处理大多数用到的是jave,但是太久没有维护更新,linux支持方面不太友好,我们可以自己封装ffmpeg进行一个媒体文件的处理工作。

下载

官网可以下载源码文件自己编译成linux和windows系统可执行的文件,但是也提供了编译好的文件(当然是其平台编译好的,你也可以自己去做这件事)。

官网:https://ffmpeg.org/

linux

image-20211022165902639

进入后根据自己的linux内核下载对应版本就可以了,右边是稳定的版本

image-20211022170008813

Windows

Windows平台同样也提供了下载可执行exe文件的地方,可自行选择下载

image-20211022170116556

命令行使用

Windows下载后可以得到这三个可执行文件,用到的是ffmpeg和ffprobe这两个文件,具体作用,图片可见。

image-20211022170410371

linux平台下载后得到内容相同。

image-20211022170619355

下载演示在Windows的使用方法:

ffprobe使用

进入到解压目录后我们就可以使用命令直接查看媒体文件的基本信息了,
通过此命令我们可以获取媒体文件的基本信息,比如说时长得信息,下边我会举例使用Jave方式获取时长信息、

查看MP4信息:ffprobe C:\Users\intel\Videos\703242828760279.mp4

image-20211022170939311

查看MP3信息:ffprobe C:\Users\intel\Videos\hahaha.mp3

image-20211022171301060

查看amr信息:ffprobe ffprobe C:\Users\intel\Videos\21141620.amr

image-20211022171335068

ffmpeg使用

首先下边的使用方法都是最基本的,可以在官网查询具体的参数使用方法。在此不做过多示例。

amr文件转成MP3

ffmpeg -i C:\Users\intel\Videos\21141620.amr -y C:\Users\intel\Videos\21141621.mp3

-y:覆盖原文件

image-20211022171748100

mp4转成avi

ffmpeg -i C:\Users\intel\Videos\703242828760279.mp4 -y C:\Users\intel\Videos\703242828760280.avi

JAVA中使用

在Java中我们可以利用ffmpeg将媒体文件进行转换,我使用场景是将amr语音文件转换成mp3格式,以供H5能够播放。

下边是提供的工具类,实现了amr转Mp3、获取媒体文件的时长。

package com.bananalab.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 媒体工具类
 *
 * @Author 李文龙
 * @Date 2021/8/3 16:54
 * @Version 1.0
 */
public class MediaUtils {

    public void amr2Mp3(File source) {
        String ffmpegPath = ffmpegPath();

        File target = null;
        try {
            target = File.createTempFile("yetiapp", ".mp3");
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (null == target) {
            return;
        }

        List<String> command = new ArrayList<>();
        command.add(ffmpegPath);
        command.add("-i");
        command.add(source.getAbsolutePath());
        command.add("-y");
        command.add(target.getAbsolutePath());

        //启动进程
        Process process = start(command);

        printRes(process);
    }

    /**
     * 获取长度
     *
     * @param file 文件
     * @return 时长
     */
    public long calcuateDuration(File file) {
        String ffprobePath = ffprobePath();

        List<String> command = new ArrayList<>();
        command.add(ffprobePath);
        command.add(file.getAbsolutePath());

        //启动进程
        Process start = start(command);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(start.getInputStream()));
        return duration(bufferedReader);
    }

    private static long duration(BufferedReader reader) {
        Pattern p1 = Pattern.compile("^\\s*Input #0, (\\w+).+$\\s*", 2);
        Pattern p2 = Pattern.compile("^\\s*Duration: (\\d\\d):(\\d\\d):(\\d\\d)\\.(\\d).*$", 2);
        Pattern p3 = Pattern.compile("^\\s*Stream #\\S+: ((?:Audio)|(?:Video)|(?:Data)): (.*)\\s*$", 2);

        while (true) {
            String line = null;
            try {
                line = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (line == null) {
                break;
            }

            System.out.println(line);

            Matcher m = p2.matcher(line);
            if (m.matches()) {
                long hours = Integer.parseInt(m.group(1));
                long minutes = Integer.parseInt(m.group(2));
                long seconds = Integer.parseInt(m.group(3));
                long dec = Integer.parseInt(m.group(4));
                return dec * 100L + seconds * 1000L + minutes * 60L * 1000L + hours * 60L * 60L * 1000L;
            }
        }

        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return 0;
    }

    private String ffmpegPath() {
        String ffmpeg = "/usr/local/ffmpeg/ffmpeg.exe";

        if (!isWindows()) {
            ffmpeg = "/usr/local/ffmpeg/ffmpeg";
        }
        return ffmpeg;
    }

    private String ffprobePath() {
        String ffprobe = "/usr/local/ffmpeg/ffprobe.exe";

        if (!isWindows()) {
            ffprobe = "/usr/local/ffmpeg/ffprobe";
        }
        return ffprobe;
    }

    private Process start(List<String> command) {
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command(command);

        //将标准输入流和错误输入流合并,通过标准输入流读取信息
        processBuilder.redirectErrorStream(true);

        //启动进程
        Process start = null;
        try {
            start = processBuilder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return start;
    }

    private void printRes(Process process) {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while (true) {
            String line = null;
            try {
                line = bufferedReader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (line == null) {
                break;
            }

            System.out.println(line);
        }

        try {
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private boolean isWindows() {
        String os = System.getProperty("os.name").toLowerCase();
        return os.contains("windows");
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值