Android实现视频剪切、视频拼接以及音视频合并
生活随笔
收集整理的這篇文章主要介紹了
Android实现视频剪切、视频拼接以及音视频合并
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
因公司項目有需求,要實現視頻剪切,視頻拼接以及音視頻合并的功能,自己通過在網上查找大量的資料終于把功能實現了,把實現的公共類提取出來,以便以后復習鞏固。
工具類
使用map4parser作為視頻處理包,android studio引入 compile 'com.googlecode.mp4parser:isoparser:1.1.21'//視頻處理
工具類
一、視頻處理工具類??
二、視頻剪切工具類
/** * 視頻剪切 * Created by lxy on 17-4-21. */ public class VideoClip {private static final String TAG = "VideoClip";private String filePath;//視頻路徑 private String workingPath;//輸出路徑 private String outName;//輸出文件名 private double startTime;//剪切起始時間 private double endTime;//剪切結束時間 public void setFilePath(String filePath) {this.filePath = filePath;}public void setWorkingPath(String workingPath) {this.workingPath = workingPath;}public void setOutName(String outName) {this.outName = outName;}public void setEndTime(double endTime) {this.endTime = endTime / 1000;}public void setStartTime(double startTime) {this.startTime = startTime / 1000;}public synchronized void clip() {try {//將要剪輯的視頻文件 Movie movie = MovieCreator.build(filePath);List<Track> tracks = movie.getTracks();movie.setTracks(new LinkedList<Track>());//時間是否修正 boolean timeCorrected = false;//計算并換算剪切時間 for (Track track : tracks) {if (track.getSyncSamples() != null && track.getSyncSamples().length > 0) {if (timeCorrected) {throw new RuntimeException("The startTime has already been corrected by another track with SyncSample. Not Supported.");}//true,false表示短截取;false,true表示長截取 startTime = VideoHelper.correctTimeToSyncSample(track, startTime, false);//修正后的開始時間 endTime = VideoHelper.correctTimeToSyncSample(track, endTime, true); //修正后的結束時間 timeCorrected = true;}}//根據換算到的開始時間和結束時間來截取視頻 for (Track track : tracks) {long currentSample = 0; //視頻截取到的當前的位置的時間 double currentTime = 0; //視頻的時間長度 double lastTime = -1; //上次截取到的最后的時間 long startSample1 = -1; //截取開始的時間 long endSample1 = -1; //截取結束的時間 //設置開始剪輯的時間和結束剪輯的時間 避免超出視頻總長 for (int i = 0; i < track.getSampleDurations().length; i++) {long delta = track.getSampleDurations()[i];if (currentTime > lastTime && currentTime <= startTime) {startSample1 = currentSample;//編輯開始的時間 }if (currentTime > lastTime && currentTime <= endTime) {endSample1 = currentSample; //編輯結束的時間 }lastTime = currentTime; //上次截取到的時間(避免在視頻最后位置了還在增加編輯結束的時間) currentTime += (double) delta/ (double) track.getTrackMetaData().getTimescale();//視頻的時間長度 currentSample++; //當前位置+1 }movie.addTrack(new CroppedTrack(track, startSample1, endSample1));// 創建一個新的視頻文件 }//合成視頻mp4 Container out = new DefaultMp4Builder().build(movie);File storagePath = new File(workingPath);storagePath.mkdirs();FileOutputStream fos = new FileOutputStream(new File(storagePath, outName));FileChannel fco = fos.getChannel();out.writeContainer(fco);//關閉流 fco.close();fos.close();} catch (Exception e) {e.printStackTrace();}}}
三、實現錄音功能的類
/** * 實現錄音功能的類 */ public class MediaRecorderUtil {private MediaRecorder mediarecorder=null;//錄音功能公共類 private static final String recordFilePath = FileUtil.getRecorderDir()+"/recorder.aac";private RecorderThread recorderThread=null;private static final String UPDATE_TAG="update_tag";/** * 開啟錄音功能 */ public void recorderStart(){//啟動midiarecoder錄音 recorderThread=new RecorderThread();recorderThread.start();}/** * 停止錄音,并保存錄音 */ public void recorderSave(){if(mediarecorder!=null){mediarecorder.stop();mediarecorder.release();mediarecorder=null;if(recorderThread!=null){recorderThread=null;}Log.e(UPDATE_TAG,"Thread stop voice and save...");}}//開啟錄音功能線程 class RecorderThread extends Thread{@Override public void run() {super.run();try {//創建保存錄音文件 File file=new File(recordFilePath);if (mediarecorder==null) {mediarecorder=new MediaRecorder();//實例化錄音文件對象 }mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//設置獲取錄音文件來源 mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);//設置錄音文件輸出格式 mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);//設置錄音文件的編碼格式 mediarecorder.setOutputFile(file.getAbsolutePath());//設置錄音文件的輸出路徑 mediarecorder.prepare();//錄音文件的準備工作 mediarecorder.start();//錄音開始 Log.e(UPDATE_TAG,"Thread start voice...");} catch (Exception e) {// TODO Auto-generated catch block e.printStackTrace();}}}}我項目主要用到的功能實現方法
一、視頻剪切主要實現方法
/** * 視頻剪切 * @param startTime 視頻剪切的開始時間 * @param endTime 視頻剪切的結束時間 * @param FilePath 被剪切視頻的路徑 * @param WorkingPath 剪切成功保存的視頻路徑 * @param fileName 剪切成功保存的文件名 */ private synchronized void cutMp4(final long startTime, final long endTime, final String FilePath, final String WorkingPath, final String fileName){new Thread(new Runnable() {@Override public void run() {try{//視頻剪切 VideoClip videoClip= new VideoClip();//實例化VideoClip類 videoClip.setFilePath(FilePath);//設置被編輯視頻的文件路徑 FileUtil.getMediaDir()+"/test/laoma3.mp4" videoClip.setWorkingPath(WorkingPath);//設置被編輯的視頻輸出路徑 FileUtil.getMediaDir() videoClip.setStartTime(startTime);//設置剪輯開始的時間 videoClip.setEndTime(endTime);//設置剪輯結束的時間 videoClip.setOutName(fileName);//設置輸出的文件名稱 videoClip.clip();//調用剪輯并保存視頻文件方法(建議作為點擊保存時的操作并加入等待對話框) }catch (Exception e){e.printStackTrace();}}}).start(); }二、開啟錄音功能
MediaRecorderUtil mrUtil=new MediaRecorderUtil(); mrUtil.recorderStart();三、音視頻合成
//aacPath錄音的文件路徑、mp4Path原視頻路徑、outPath合成之后輸出的視頻文件路徑
boolean flag=Mp4ParseUtil.muxAacMp4(aacPath,mp4Path,outPath);四、視頻拼接
//mMp4List視頻拼接的路徑集合、outPath拼接成功的視頻輸出路徑
大致就這樣了
我不是個呆若木雞的小小英
總結
以上是生活随笔為你收集整理的Android实现视频剪切、视频拼接以及音视频合并的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为了让机器能和人更好的聊天, Googl
- 下一篇: vc下文件下载的两种方法