音频文件的上传页面
注:上传组件使用web uploader组件
<div id="merge_picker">上传文件div>
<div id="merge_audio">div>
<button type="button" onclick="mergeAudio()">合并button>
<link href="$path/js/webuploader-0.1.5/webuploader.css" rel="stylesheet" type="text/css"/>
<script src="$path/js/webuploader-0.1.5/webuploader.min.js">script>
<script type="text/javascript">
$(document).ready(function(e) {
// 初始化Web Uploader
var uploader = WebUploader.create({
// 选完文件后,是否自动上传。
auto: true,
// swf文件路径
swf: '$!path/js/webuploader-0.1.5/Uploader.swf',
// 文件接收服务端。
server: '$!path/text/uploadaudio',
// 内部根据当前运行是创建,可能是input元素,也可能是flash.
pick: '#merge_picker',
fileVal:'Filedata',
//同一文件是否可以重复上传
duplicate :true,
// 只允许选择图片文件。
accept: {
title: 'audio',
extensions: 'mp3,wav',
mimeTypes: 'audio/*'
}
});
//上传过程中
uploader.on('uploadProgress',function( file, percentage ) {
$(".bs-example-modal-sm").modal("show");
});
//上传结束
uploader.on( 'uploadComplete', function( file ) {
$(".bs-example-modal-sm").modal("hide");;
});
// 完成上传完了,成功或者失败,先删除进度条。
uploader.on( 'uploadSuccess', function(file,resp) {
var url = '$!path/' + resp.url;
var audio = '';
var input = ''" type="hidden">';
$("#merge_audio").append(audio+input);
});
});
function mergeAudio(){
var audio_path = "";
$(".merge_audio_path").each(function(){
var merge_audio_path = $(this).val();
if(audio_path==""){
audio_path = merge_audio_path;
}else{
audio_path = audio_path + ";" + merge_audio_path;
}
});
$.ajax({
type:'post',
data:{audioPath:audio_path},
url:'$path/text/mergeaudio',
success:function(date){
var url = "$path/" + date;
var merge_audio = '';
$("#merge_audio").html(merge_audio);
}
});
}
script>
java代码 音频文件上传
@Post("uploadaudio")
public Object uploadaudio(Invocation inv, Model model, MultipartFile Filedata){
String uploadDir = config.getString("upload.audio");
int maxDirCount = config.getInt("upload.maxDirCount", 100);
HashMap<String,Object> map = uploadVideo(inv, Filedata, uploadDir, maxDirCount);
model.add("result", map);
return "result.json";
}
public static HashMap<String,Object> uploadVideo(Invocation inv, MultipartFile Filedata,
String uploadDir,Integer maxDirCount){
String urls = new String();
String url = null;
//Filedata-上传文件;uploadDir-上传路径;
String urlDir = uploadDir;
uploadDir = inv.getServletContext().getRealPath(uploadDir);
long tmp = System.currentTimeMillis();
long dirName = (tmp % new Long(maxDirCount));
urlDir = urlDir + "/" + dirName;
File dir = new File(uploadDir, String.valueOf(dirName));
if (!dir.exists()) {
dir.mkdirs();
}
String extName = FilenameUtils.getExtension(Filedata
.getOriginalFilename());
if (StringUtils.isEmpty(extName)) {
String type = Filedata.getContentType();
extName = MimeTypes.getExtension(type);
extName = StringUtils.substring(extName, 1);
}
String mainFileName = FilenameUtils.getBaseName(Filedata
.getOriginalFilename());
String fileName = mainFileName + "." + extName;
File file = new File(dir, fileName);
String path = urlDir + "/" + fileName;
try {
Filedata.transferTo(file);
Thumbnails.of(file);
urls = path;
url = ArrayUtils.toString(urls);
url = StringUtils.removeStart(url, "{");
url = StringUtils.removeEnd(url, "}");
} catch (Exception e) {
e.printStackTrace(System.err);
}
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("url", url);
return map;
}
音频文件合并
/**
* 合并音频文件
* @param inv
* @param model
* @param audioPaths
* @return
*/
@Post("mergeaudio")
public Object mergeAudio(Invocation inv,Model model,String audioPath){
String upOutDir = config.getString("upload.audio");
//剪辑后的路径
upOutDir = inv.getServletContext().getRealPath(upOutDir);
//获取年和月的时间
DateFormat dft = new SimpleDateFormat("yyyyMM");
Timestamp timestamp = new Timestamp((new Date()).getTime());
String outFile = upOutDir + File.separator +dft.format(timestamp);
File file = new File(outFile);
if(!file.exists()){
file.mkdirs();
}
List> maps = new ArrayList>();
String[] audioPaths = audioPath.split(";");
for(int i=0;i map = new HashMap();
String path = inv.getServletContext().getRealPath(audioPaths[i]);
map.put("path", path);
map.put("start", 0);
map.put("end", 0);
maps.add(map);
// int number = audioPaths[i].split("\\.").length-1;
}
String musicName = timestamp.getTime() + ".mp3";
upOutDir = outFile+File.separator + musicName;
mergeAudio(maps, upOutDir);
String path = config.getString("upload.audio")+"/"+dft.format(timestamp)+"/"+musicName;
model.add("result", path);
return "result.json";
}
/**
* 合并音频文件
* @param maps
* @param output
*/
public void mergeAudio(List> maps,String output){
BufferedOutputStream bos = null;
//目标文件路径
File target = new File(output);
for(int i=0;itry{
String source = maps.get(i).get("path")+"";
File file = new File(source);
long totalTime = getVideoTime(source, "E:\\ffmpeg-20170425-b4330a0-win32-static\\ffmpeg-20170425-b4330a0-win32-static\\bin\\ffmpeg.exe"); //总时长(秒)
int start = Integer.valueOf(maps.get(i).get("start").toString());
int end = Integer.valueOf(maps.get(i).get("end").toString());
if(start<0 || end<=0 || start>=totalTime || end>totalTime || start>=end){
start = 0;
end = (int) totalTime;
}
FileInputStream fis = new FileInputStream(file);
long fileSize = file.length(); //音频数据大小
long splitSize = (fileSize/totalTime)*(end-start); //截取的音频数据大小
long skipSize = (fileSize/totalTime)*start; //截取时跳过的音频数据大小
int splitSizeInt = Integer.parseInt(String.valueOf(splitSize));
int skipSizeInt = Integer.parseInt(String.valueOf(skipSize));
byte[] fbyte = new byte[splitSizeInt]; //存放截取的音频数据
byte[] skipBytes = new byte[skipSizeInt]; //存放截取时跳过的音频数据
fis.read(skipBytes, 0, skipBytes.length); //跳过不需要截取的数据
fis.read(fbyte, 0, fbyte.length); //读取要截取的数据到目标数组
fis.close();
if(target.exists()){ //如果目标文件已存在,则删除目标文件
target.delete();
}
//缓冲字节输出流(true表示可以在流的后面追加数据,而不是覆盖!!)
bos = new BufferedOutputStream(new FileOutputStream(target, true));
bos.write(fbyte);
bos.flush();
System.out.println("第"+i+"首剪辑完成");
}catch (Exception e) {
e.printStackTrace();
}
}
//切记要关闭流!!
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 获取音频文件总时长
* @param video_path 文件路径
* @param ffmpeg_path 工具路径
* @return
*/
public int getVideoTime(String video_path, String ffmpeg_path) {
List commands = new java.util.ArrayList();
commands.add(ffmpeg_path);
commands.add("-i");
commands.add(video_path);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commands);
final Process p = builder.start();
//从输入流中读取视频信息
BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
//从视频信息中解析时长
String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
Pattern pattern = Pattern.compile(regexDuration);
Matcher m = pattern.matcher(sb.toString());
if (m.find()) {
int time = getTimelen(m.group(1));
return time;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
//格式:"00:00:10.68"
private int getTimelen(String timelen){
int min=0;
String strs[] = timelen.split(":");
if (strs[0].compareTo("0") > 0) {
min+=Integer.valueOf(strs[0])*60*60;//秒
}
if(strs[1].compareTo("0")>0){
min+=Integer.valueOf(strs[1])*60;
}
if(strs[2].compareTo("0")>0){
min+=Math.round(Float.valueOf(strs[2]));
}
return min;
}
在剪辑和合并音频文件时,需要下载ffmpeg.exe工具。