图片转视频(jpg->mov),视频转码(mov->mp4)Java实现

package imgTivideo;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;

import org.jim2mov.core.DefaultMovieInfoProvider;
import org.jim2mov.core.ImageProvider;
import org.jim2mov.core.Jim2Mov;
import org.jim2mov.core.MovieInfoProvider;
import org.jim2mov.core.MovieSaveException;
import org.jim2mov.utils.MovieUtils;
import ws.schild.jave.*;
import ws.schild.jave.encode.EncodingAttributes;
import ws.schild.jave.encode.VideoAttributes;
import ws.schild.jave.info.MultimediaInfo;
import ws.schild.jave.info.VideoInfo;
import ws.schild.jave.info.VideoSize;

public class imgTovideo {
    public static void convertPicToAvi(String jpgDirPath, String aviFileName, int fps, int mWidth, int mHeight) {
        final File[] jpgs = new File(jpgDirPath).listFiles();
        if (jpgs == null || jpgs.length == 0) {
            return;
        }
        // 生成视频的名称
        DefaultMovieInfoProvider dmip = new DefaultMovieInfoProvider(aviFileName);
        // 设置每秒帧数
        dmip.setFPS(fps); 
        // 设置总帧数
        dmip.setNumberOfFrames(jpgs.length);
        // 设置视频宽和高(最好与图片宽高保持一直)
        dmip.setMWidth(mWidth); 
        dmip.setMHeight(mHeight); 
        try {
            new Jim2Mov(new ImageProvider() {
                @Override
                public byte[] getImage(int frame) {
                    try {
                        // 设置压缩比
                        return MovieUtils.convertImageToJPEG((jpgs[frame]), 1.0f);
                    } catch (IOException e) {
                        System.err.println(e);
                    }
                    return null;
                }
            }, dmip, null).saveMovie(MovieInfoProvider.TYPE_QUICKTIME_JPEG);
        } catch (MovieSaveException e) {
            System.err.println(e);
        }
        System.out.println("视频生成成功");
    }
    public void convertVideoToMP4(File source, String targetPath) {
        MultimediaObject multimediaObject = new MultimediaObject(source);
        try {
            MultimediaInfo info = multimediaObject.getInfo();
            VideoInfo videoInfo = info.getVideo();
            VideoSize size = videoInfo.getSize();
            Integer bitRate = size.getWidth()*size.getHeight();
            VideoAttributes video = new VideoAttributes();
            //设置视频编码
            video.setCodec("h264");
            if (ObjectUtil.isNotNull(bitRate)) {
                //设置比特率
                video.setBitRate(bitRate * 1000);
            }
            File target = new File(targetPath);
            EncodingAttributes attrs = new EncodingAttributes();
            //设置转换后的格式
            attrs.setOutputFormat("mp4");
            attrs.setVideoAttributes(video);
            Encoder encoder = new Encoder();
            encoder.encode(multimediaObject, target, attrs);
        } catch (EncoderException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws Exception {
        String jpgDirPath = "d:/imgs/";// 图片存放的地方,建议放jpg或者jpeg,不要将其他的图片格式转码成jpg,测试的时候我出问题了
        String aviFileName = "test.mov";//在当前目录生成mov视频
        imgTovideo.convertPicToAvi(jpgDirPath, aviFileName, 25, 1280, 720);
        File file = new File("D:/imgTovideo/test.mov");
        imgTovideo itov = new imgTovideo();
        itov.convertVideoToMP4(file, "demo.mp4");//视频转码后可在chrome浏览器上播放
    }
}

pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>imgTovideo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>7</source>
                    <target>7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
    <!-- https://mvnrepository.com/artifact/javax.media/jmf -->
    <dependency>
        <groupId>javax.media</groupId>
        <artifactId>jmf</artifactId>
        <version>2.1.1e</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.bytedeco/javacv -->
    <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacv</artifactId>
        <version>1.5.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.bytedeco/javacpp -->
    <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacpp</artifactId>
        <version>1.5.4</version>
    </dependency>
        <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.5.7</version>
        </dependency>
        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-nativebin-win64</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-nativebin-linux64</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
        <groupId>ws.schild</groupId>
        <artifactId>jave-core</artifactId>
        <version>3.0.1</version>
    </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>aliyun</id>
            <name>aliyun Repository</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

其他还需要一个Jim2mov的jar包,测试环境是windows。
Jim2Mov是依赖jmf包的
点击直达 -> 图片转视频的完美版,亲测有效

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值