怎么使用ffmpeg实现视频转视频

发布时间:2021-11-17 09:14:56 作者:iii
来源:亿速云 阅读:187

本篇内容主要讲解“怎么使用ffmpeg实现视频转视频”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用ffmpeg实现视频转视频”吧!

视频转视频

主要用到了FFmpeg这个工具,利用命令对视频文件进行操作。首先根据自己调的参数进行图片的截取(本文的是1秒10帧的参数),图片转换,然后分离音频,最后字符图片和音频合成目标视频。

FFmpeg的代码库:

https://github.com/FFmpeg/FFmpeg

FFmpeg下载地址:

https://ffmpeg.org/download.html

本位使用的版本:

https://blog-lossingdawn.oss-cn-shanghai.aliyuncs.com/img2text/ffmpeg-20180201-b1af0e2-win64-static.zip

效果如下

没做细致的调整,视频的转换可以调调参数

怎么使用ffmpeg实现视频转视频

怎么使用ffmpeg实现视频转视频

测试代码

    @Test
    public static void videoTest() {
        String srcVideoPath = "F:/123/123.mp4";
        String tarImagePath = "F:/123/mp/";
        String tarAudioPath = "F:/123/mp/audio.aac";
        String tarVideoPath = "F:/123/1234.mp4";
        VideoUtil.readVideo(srcVideoPath,tarImagePath,tarAudioPath,tarVideoPath);
    }

代码如下

主要用到的几个命令,其他按帧截图命令参考文末链接4:

// 截图
ffmpeg -ss 10 -i input.flv -y -f image2  -vframes 100 -s 352x240 b-%03d.jpg
// 分离音频
ffmpeg -i 3.mp4 -vn -y -acodec copy 3.aac
// 合成视频
ffmpeg -threads2 -y -r 10 -i /tmpdir/image%04d.jpg -i audio.mp3 -absf aac_adtstoasc output.mp4

环境:

JDK 1.8

完整代码如下

// ffmpeg -ss 10 -i input.flv -y -f image2  -vframes 100 -s 352x240 b-%03d.jpg  
    /**
     * ffmpeg 截图,并指定图片的大小
     * 
     * @param srcVideoPath
     * @param tarImagePath
     *            截取后图片路径
     * @param width
     *            截图的宽
     * @param hight
     *            截图的高
     * @param offsetValue
     *            表示相对于文件开始处的时间偏移值 可以是分秒
     * @param vframes
     *            表示截图的桢数
     * 
     * @return
     */
    public static boolean processFfmpegImage(String srcVideoPath, String tarImagePath, int width, int hight,
            float offsetValue, float vframes) {
        if (!checkfile(srcVideoPath)) {
            System.out.println("【" + srcVideoPath + "】  不存在 !");
            // logger.error("【" + srcVideoPath + "】 不存在 !");
            return false;
        }
        List<string> commend = new java.util.ArrayList<string>();

        commend.add(ffmpegPath);

        commend.add("-i");

        commend.add(srcVideoPath);

        commend.add("-y");

        commend.add("-f");

        commend.add("image2");

        commend.add("-ss");

        commend.add(offsetValue + ""); // 在视频的某个插入时间截图,例子为5秒后

        // commend.add("-vframes");

        commend.add("-t");// 添加参数"-t",该参数指定持续时间

        commend.add(vframes + ""); // 截图的桢数,添加持续时间为1毫秒

        commend.add("-s");

        commend.add(width + "x" + hight); // 截图的的大小

        commend.add(tarImagePath);

        try {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commend);
            builder.redirectErrorStream(true);
            // builder.redirectOutput(new File("F:/123/log/log.log"));
            Process process = builder.start();
            doWaitFor(process);
            process.destroy();
            if (!checkfile(tarImagePath)) {
                System.out.println(tarImagePath + " is not exit!  processFfmpegImage 转换不成功 !");
                // logger.info(tarImagePath + " is not exit! processFfmpegImage
                // 转换不成功 !");
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("【" + srcVideoPath + "】 processFfmpegImage  转换不成功 !");
            // logger.error("【" + srcVideoPath + "】 processFfmpegImage 转换不成功
            // !");
            return false;
        }
    }
    
    public static boolean processFfmpegAudio(String srcVideoPath, String tarAudioPath) {
        if (!checkfile(srcVideoPath)) {
            System.out.println("【" + srcVideoPath + "】  不存在 !");
            // logger.error("【" + srcVideoPath + "】 不存在 !");
            return false;
        }
        // https://blog.csdn.net/xiaocao9903/article/details/53420519
        // ffmpeg -i 3.mp4 -vn -y -acodec copy 3.aac
        // ffmpeg -i 3.mp4 -vn -y -acodec copy 3.m4a
        
        List<string> commend = new java.util.ArrayList<string>();
        
        commend.add(ffmpegPath);
        
        commend.add("-i");
        
        commend.add(srcVideoPath);
        
        commend.add("-vn");

        commend.add("-y");
        
        commend.add("-acodec");
        
        commend.add("copy"); // 在视频的某个插入时间截图,例子为5秒后
        
        commend.add(tarAudioPath);
        
        try {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commend);
            builder.redirectErrorStream(true);
            Process process = builder.start();
            doWaitFor(process);
            process.destroy();
            if (!checkfile(tarAudioPath)) {
                System.out.println(tarAudioPath + " is not exit!  processFfmpegAudio 转换不成功 !");
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("【" + srcVideoPath + "】 processFfmpegAudio  转换不成功 !");
            return false;
        }
    }

    /**
     * ffmpeg 合成视频
     * 
     * @param srcVideoPath
     * @param tarImagePath
     *            截取后图片路径
     * @param width
     *            截图的宽
     * @param hight
     *            截图的高
     * @param offsetValue
     *            表示相对于文件开始处的时间偏移值 可以是分秒
     * @param vframes
     *            表示截图的桢数
     * 
     * @return
     */
    public static boolean processFfmpegVideo(String imagePath, String audioPath, String tarVideoPath, int step) {
        // https://blog.csdn.net/wangshuainan/article/details/77914508?fps=1&amp;locationNum=4
        // 带音频
        // ffmpeg -threads2 -y -r 10 -i /tmpdir/image%04d.jpg -i audio.mp3 -absf
        // aac_adtstoasc output.mp4

        List<string> commend = new java.util.ArrayList<string>();

        commend.add(ffmpegPath);

        commend.add("-threads");
        
        commend.add("2");

        commend.add("-y");

        commend.add("-r");

        commend.add(step + "");

        commend.add("-i");

        commend.add(imagePath); // 图片

        commend.add("-i");

         commend.add(audioPath);

        commend.add("-absf");// 

        commend.add("aac_adtstoasc"); // 

        commend.add(tarVideoPath);

        try {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commend);
            builder.redirectErrorStream(true);
             builder.redirectOutput(new File("F:/123/log/log.log"));
            Process process = builder.start();
            doWaitFor(process);
            process.destroy();
            if (!checkfile(tarVideoPath)) {
                System.out.println(tarVideoPath + " is not exit!  processFfmpegVideo 转换不成功 !");
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("【" + tarVideoPath + "】 processFfmpegVideo  转换不成功 !");
            return false;
        }
    }

到此,相信大家对“怎么使用ffmpeg实现视频转视频”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. 怎么在java中使用ffmpeg处理视频
  2. Java使用ffmpeg和mencoder实现视频转码

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ffmpeg

上一篇:如何解决vc升级失败恢复快照后数据不一致问题

下一篇:jquery如何获取tr里面有几个td

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》