Java代码如何获取视频时长、宽高、文件大小、格式、截图呢?
下文笔者讲述java中获取视频时长,视频大小,视频格式等相关信息简介说明,如下所示
实现思路: 1.下载相应的jar包 http://www.sauronsoftware.it/projects/jave/download.php 2.借助it.sauronsoftware.jave.MultimediaInfo类中的 相关方法,即可获取视频的相关信息例:
import it.sauronsoftware.jave.Encoder; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; import java.nio.channels.FileChannel; /** * @Description:获取视频宽高大小时间 */ public class ReadVideo { public static void main(String[] args) { File source = new File("D://testmp4.mp4"); Encoder encoder = new Encoder(); FileChannel fc = null; String size = ""; try { it.sauronsoftware.jave.MultimediaInfo m = encoder.getInfo(source); long ls = m.getDuration(); System.out.println("此视频时长为:" + ls / 60000 + "分" + (ls) / 1000 + "秒!"); // 视频帧宽高 System.out.println("此视频高度为:" + m.getVideo().getSize().getHeight()); System.out.println("此视频宽度为:" + m.getVideo().getSize().getWidth()); System.out.println("此视频格式为:" + m.getFormat()); FileInputStream fis = new FileInputStream(source); fc = fis.getChannel(); BigDecimal fileSize = new BigDecimal(fc.size()); size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB"; System.out.println("此视频大小为" + size); } catch (Exception e) { e.printStackTrace(); } finally { if (null != fc) { try { fc.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
对视频进行截图
public static boolean processImg(String veido_path,String ffmpeg_path) { File file = new File(veido_path); if (!file.exists()) { System.err.println("路径[" + veido_path + "]对应的视频文件不存在!"); return false; } list<String> commands = new java.util.ArrayList<String>(); commands.add(ffmpeg_path); commands.add("-i"); commands.add(veido_path); commands.add("-y"); commands.add("-f"); commands.add("image2"); commands.add("-ss"); commands.add("1");//这个参数是设置截取视频多少秒时的画面 commands.add("-t"); commands.add("0.001"); commands.add("-s"); commands.add("1920x1080");//宽X高 commands.add(veido_path.substring(0, veido_path.lastIndexOf(".")).replaceFirst("vedio", "file") + ".jpg"); try { ProcessBuilder builder = new ProcessBuilder(); builder.command(commands); builder.start(); System.out.println("截取成功"); return true; } catch (Exception e) { e.printStackTrace(); return false; } } public static void main(String[] args) { processImg("C:\\Users\\java265\\Desktop\\hellTestMp4.mp4","src/test/lib/ffmpeg.exe"); } 注意事项: 此处借助了ffmpeg.exe这个程序进行截图操作 ffmpeg.exe应用程序的下载位置 http://ffmpeg.zeranoe.com/builds/win64/static/ffmpeg-20170921-183fd30-win64-static.zip
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。