标签:into 电脑 运行 res 简单 bis dev 文件 ash
方法一:
在项目开发过程中,需要获取音视频文件时长。查询资料后发现 JAVE能够完美得到想要的结果,JAVE项目简介如下:
The JAVE (Java Audio Video Encoder) library is Java wrapper on the ffmpeg project. Developers can take take advantage of JAVE to transcode audio and video files from a format to another. In example you can transcode an AVI file to a MPEG one, you can change a DivX video stream into a (youtube like) Flash FLV one, you can convert a WAV audio file to a MP3 or a Ogg Vorbis one, you can separate and transcode audio and video tracks, you can resize videos, changing their sizes and proportions and so on. Many other formats, containers and operations are supported by JAVE.
总的来说JAVE不仅能够获取音视频文件时长,而且能够在代码中将音视频进行格式转换,可以说功能很强大了,我暂时只用到获取时长这一最简单的功能。
首先,下载jar包,点此下载;
然后,通过maven进行安装(首先确保电脑已经配置好maven环境变量):
步骤:1.进入maven仓库目录,一般目录为C:\Users\Administrator\.m2\repository,亦可自己创建仓库,创建后需要在maven的conf目录下修改settings.xml文件中的localRepository的配置;
2.将jave-1.0.2.jar包复制到仓库目录下;
3.win键 + R 打开运行对话框,输入cmd进入Command,进入仓库目录,执行以下命令:
mvn install:install-file -Dfile=jave-1.0.2.jar -DgroupId=jave -DartifactId=jave -Dversion=1.0.2 -Dpackaging=jar -DgeneratePom=true
然后在maven项目中就可以通过maven导入jar包了。
简单的测试用例:
package com.jave;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;
import java.io.File;
public class ReadVideo {
public static void main(String[] args) {
File source = new File("E:\\测试视频\\R41.avi");
Encoder encoder = new Encoder();
try {
MultimediaInfo m = encoder.getInfo(source);
long ls = m.getDuration();
int second = ls \ 1000;
System.out.println("此视频时长为:" + second + "秒!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
转自https://blog.csdn.net/xie_sining/article/details/79643152
方法二:
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.io.File;
import java.net.URL;
import java.util.Date;
/**
* @author liuziw
* @date 2019/8/21 9:17
*/
public class AudioTimeTest {
/**
* 获取音频文件时长
*
* @param wavFilePath wav文件路径,支持本地和网络HTTP路径
* @return 时长/微秒,可 /1000000D 得到秒
* @throws Exception
*/
public static long getMicrosecondLengthForWav(String wavFilePath) throws Exception {
if (wavFilePath == null || wavFilePath.length() == 0) {
return 0;
}
String bath = wavFilePath.split(":")[0];
Clip clip = AudioSystem.getClip();
AudioInputStream ais;
if ("http".equals(bath.toLowerCase())||"https".equals(bath.toLowerCase())) {
ais = AudioSystem.getAudioInputStream(new URL(wavFilePath));
} else {
ais = AudioSystem.getAudioInputStream(new File(wavFilePath));
}
clip.open(ais);
return clip.getMicrosecondLength();
}
public static void main(String [] args)throws Exception {
Long start = new Date().getTime();
String url = "";
Long time = getMicrosecondLengthForWav(url)/1000000;
Long end = new Date().getTime();
System.out.println("花费(毫秒)"+(end-start));
System.out.println("时长为(秒)"+time);
}
}
标签:into 电脑 运行 res 简单 bis dev 文件 ash
原文地址:https://www.cnblogs.com/luizw/p/11387459.html