用java做一个简单的视频转码器

用java做一个视频转码器

本Markdown编辑器使用[StackEdit][6]修改而来,用它写博客,将会带来全新的体验哦:
用java做一个视频转码器,首先实现视频格式转换,需要用到两个软件”ffmpeg”和”mencoder”话不多说,直接上代码:

//创建一个Contants类,统一管理路径
public class Contants {
   
    public static final String ffmpegpath = "D:\\ffmpeg\\ffmpeg-20180521-c24d247-win64-static\\bin\\ffmpeg.exe";//ffmpeg的安装位置
    public static final String mencoderpath = "D:\\ffmpeg\\MPlayer-x86_64-r38022+gdb2a7c947e\\mencoder.exe"; // mencoder的目录

    public static final String videofolder = "D:\\tools\\video\\"; // 需要被转换格式的视频目录
    public static final String targetfolder = "D:\\tools\\target\\"; // 转换后视频的目录

    public static final String videoRealPath = "D:\\tools\\target\\"; // 需要被截图的视频目录;
    public static final String imageRealPath = "D:\\tools\\imgs\\"; // 截图的存放目录
}

这是转换视频格式的工具类,可以直接使用

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class ConverVideoUtils {
   
    private Date dt;
    private long begintime;
    private String sourceVideoPath;//源视频路径
    private String filerealname; // 文件名 不包括扩展名
    private String filename; // 包括扩展名
    private String videofolder = Contants.videofolder; // 别的格式视频的目录
    private String targetfolder = Contants.targetfolder; // flv视频的目录
    private String ffmpegpath = Contants.ffmpegpath; // ffmpeg.exe的目录
    private String mencoderpath = Contants.mencoderpath; // mencoder的目录
    private String imageRealPath = Contants.imageRealPath; // 截图的存放目录

    public ConverVideoUtils() {
    }

    public ConverVideoUtils(String path) {
        sourceVideoPath = path;
    }

    public String getPATH() {
        return sourceVideoPath;
    }

    public void setPATH(String path) {
        sourceVideoPath = path;
    }

    /**
     * 转换视频格式
     * @param  targetExtension 目标视频扩展名 .xxx
     * @param isDelSourseFile 转换完成后是否删除源文件
     * @return
     */
    public boolean beginConver(String targetExtension, boolean isDelSourseFile) {
        File fi = new File(sourceVideoPath);
        filename = fi.getName();
        filerealname = filename.substring(0, filename.lastIndexOf(".")).toLowerCase();
        System.out.println("----接收到文件(" + sourceVideoPath + ")需要转换-------------------------- ");
        if (!checkfile(sourceVideoPath)) {
            System.out.println(sourceVideoPath + "文件不存在" + " ");
            return false;
        }
        dt = new Date();
        begintime = dt.getTime();
        System.out.println("----开始转文件(" + sourceVideoPath + ")-------------------------- ");
        if (process(targetExtension,isDelSourseFile)) {
            Date dt2 = new Date();
            System.out.println("转换成功 ");
            long endtime = dt2.getTime();
            long timecha = (endtime - begintime);
            String totaltime = sumTime(timecha);
            System.out.println("转换视频格式共用了:" + totaltime + " ");
            /*调用截图方法
            if (processImg(sourceVideoPath)) {
                System.out.println("截图成功了! ");
            } else {
                System.out.println("截图失败了! ");
            }*/
            /*调用删除方法
            if (isDelSourseFile) {
                deleteFile(sourceVideoPath);
            }*/
            sourceVideoPath = null;
            return true;
        } else {
            sourceVideoPath = null;
            return false;
        }
    }

    /**
     * 对视频进行截图
     * @param sourceVideoPath 需要被截图的视频路径(包含文件名和扩展名)
     * @return
     */
    public boolean processImg(String sourceVideoPath) {
        if (!checkfile(sourceVideoPath)) {
            System.out.println(sourceVideoPath + " is not file");
            return false;
        }
        File fi = new File(sourceVideoPath);
        filename = fi.getName();
        filerealname = filename.substring(0, filename.lastIndexOf(".")).toLowerCase();
        List<String> commend = new java.util.ArrayList<String>();
        //第一帧: 00:00:01
   
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值