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包的
点击直达 -> 图片转视频的完美版,亲测有效