基于ffmpeg的ws.schild的工具包给视频截图,silk音频格式转mp3

功能简介

ffmpeg音视频转换工具,需要独立安装;这里使用ws.schild(ffmpeg)java的工具进行对视频的封面截图;主要就是方便随项目直接打包部署也不用添加任何配置操作。

silk音频文件转mp3,我接触到是微信相关集成开发的时候用到,这里也记录下方便大家拿走直接用。

Dome简介

基于ffmpeg的ws.schild的工具包给视频截图,silk音频格式转mp3

dome截图

  • FFmpegUtil封装的一个帮助类,处理视频的截图、音频的转换等。
  • silk-v3-decoder文件夹是silk音频文件转mp3的工具;这里帮助类里面是取项目路径,所以jar部署的时候将silk-v3-decoder文件夹和jar放在同级目录就可以了。

助力划水 源码(腾讯工蜂):https://git.code.tencent.com/lhb316/ffmpeg-dome

搬砖程序的痛苦莫过于搜索到了方法居然只有代码片段,一堆大道理底层代码说明;然后根据代码片段写出的代码居然缺少各种引用,然后无从下手,最终代码跑不通。需求、功能、问题还是没得到解决。

pom.xml添加ws.schild引用



    ws.schild
    jave-all-deps
    3.3.1

FFmpegUtil帮助类相关方法

  • 视频截取封面图
/**
     * 截取视频中某一帧作为图片
     *
     * @param videoPath 视频文件路径
     * @param imagePath 音频文件路径
     * @return
     */
    public static boolean getVideoProcessImage(String videoPath, String imagePath) {
        File videoSource = new File(videoPath);
        File imageTarget = new File(imagePath);
        MultimediaObject object = new MultimediaObject(videoSource);
        try {
            MultimediaInfo multimediaInfo = object.getInfo();
            VideoInfo videoInfo = multimediaInfo.getVideo();
            Map metadata = videoInfo.getMetadata();
            String rotate = metadata.getOrDefault("rotate", "");
            VideoSize vs = videoInfo.getSize();
//            if (rotate) {
//                vs = new VideoSize(videoInfo.getSize().getHeight(), videoInfo.getSize().getWidth());
//            }
            VideoAttributes video = new VideoAttributes();
            video.setCodec("png");
            video.setSize(vs);
            EncodingAttributes attrs = new EncodingAttributes();
            //VideoAttributes attrs = ecodeAttrs.getVideoAttributes().get();
            attrs.setOutputFormat("image2");
            attrs.setDuration(0.01f);//设置转码持续时间(1秒)
            attrs.setVideoAttributes(video);
            Encoder encoder = new Encoder();
            encoder.encode(object, imageTarget, attrs);
            //  C:\Users\ADMINI~1\AppData\Local\Temp\jave\ffmpeg-amd64-3.2.0.exe -i
            //  E:\Download\111.mp4 -t 0.01 -vcodec png -s 1440x720 -movflags faststart -an -f image2 -y
            //  E:\Download\111.png -hide_banner
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
  • silk音频格式转mp3
/**
 * silk文件转mp3文件
 *
 * @param silkPath silk文件
 * @param pcmPath  解码为pcm文件
 * @param mp3Path  mp3文件
 * @return
 */
public static boolean silk2mp3(String silkPath, String pcmPath, String mp3Path) {
    boolean bl = silk2Pcm(silkPath, pcmPath);
    if (bl) {
        return pcm2Mp3(pcmPath, mp3Path);
    } else {
        return false;
    }
}

/**
 * 解码为pcm格式
 */
public static boolean silk2Pcm(String silkPath, String pcmPath) {
    Process process = null;
    try {
        String silkv3Path = "";
        String command = "";
        if (FileUtil.isWindows()) {
            silkv3Path = getProPath() + File.separator + "silk-v3-decoder" + File.separator + "windows" + File.separator + "silk_v3_decoder.exe";
            command = "cmd.exe /c " + silkv3Path + " " + silkPath + " " + pcmPath + " -quiet";
        } else {
            silkv3Path = getProPath() + File.separator + "silk-v3-decoder" + File.separator + "converter.sh";
            command = "sh " + silkv3Path + " " + silkPath + " " + pcmPath;
        }
        process = RuntimeUtil.exec(command);
        int i = process.waitFor();
        return i == 0;
    } catch (Exception e) {
        log.error("silk2Pcm异常:" + e.getMessage(), e);
        return false;
    } finally {
        RuntimeUtil.destroy(process);
    }
}

/**
 * pcm为MP3格式
 */
public static boolean pcm2Mp3(String pcm, String mp3) {
    Process process = null;
    try {
        DefaultFFMPEGLocator ffmpeg = new DefaultFFMPEGLocator();
        String command = "";
        if (FileUtil.isWindows()) {
            command = "cmd.exe /c " + ffmpeg.getExecutablePath() + " -y -f s16le -ar 24000 -ac 1 -i " + pcm + " " + mp3;
        } else {
            command = ffmpeg.getExecutablePath() + " -y -f s16le -ar 24000 -ac 1 -i " + pcm + " " + mp3;
        }
        process = RuntimeUtil.exec(command);
        int i = process.waitFor();
        return i == 0;
    } catch (Exception e) {
        log.error("pcm2Mp3异常:" + e.getMessage(), e);
        return false;
    } finally {
        RuntimeUtil.destroy(process);
    }
}


public static String getProPath() throws FileNotFoundException {
    File path = new File(ResourceUtils.getURL("classpath:").getPath());
    if (!path.exists()) {
        path = new File("");
    }
    return path.getAbsolutePath();
}

测试方法

Swagger访问网址: http://ip:port/doc.html

  • 视频文件上传操作截取封面图
基于ffmpeg的ws.schild的工具包给视频截图,silk音频格式转mp3

视频文件截图

  • silk音频文件上传转mp3文件
基于ffmpeg的ws.schild的工具包给视频截图,silk音频格式转mp3

silk音频文件转mp3

结语

文章中我只截出了关键性的代码方法,相信对需要的人提供帮助或参考。

助力划水 源码(腾讯工蜂):https://git.code.tencent.com/lhb316/ffmpeg-dome

希望需要的程序员可以直接使用CTRL + V、CTRL + C就能实现你的需求那是我写这篇文章最大的成就。

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章