老师为什么我上传抖音的视频然后搭上自己的bgm后生成的新视频没有效果呢
来源:6-21 小程序上传视频后调用视频处理工具类联调

寿喜烧爱好者
2020-03-20
要合并的bgm并没有在新视频中播放出来,依旧是原来抖音的原声。而且我一下子生成了两个视频
写回答
3回答
-
六维世界
2020-03-26
我也是同样的问题,添加bgm没有效果,百度找了一些解决方案,最后繁琐的解决了(java代码都一样,这里只给出FFmpeg命令):
1. 先提取出原视频的音频
ffmpeg -i input.mp4 -vn -y -acodec copy output.m4a
2. 合并bgm与视频中的音频
ffmpeg -i output.m4a -i bgm.mp3 -filter_complex amerge -ac 2 -c:a libmp3lame -q:a 4 output.mp3
3. 将合并后的音频 与 视频合并 并覆盖视频中的音频
ffmpeg.exe -i input.mp4 -i output.mp3 -t 7 -y -map 0:v:0 -map 1:a:0 新的视频.mp4
原文链接:https://www.jianshu.com/p/2a824f13b2af
252021-05-04 -
六维世界
2020-03-27
// 根据自己情况改动,我这里的代码可以当做一个参考
public class MergeVideoMp3 { private String ffmpegEXE; private String tempAudioPath = "E:\\tool\\tempAudio.m4a"; private String tempBgmPath = "E:\\tool\\tempBgm.mp3"; private String tempCoverPath = "E:\\tool\\tempCover.jpg"; public MergeVideoMp3(String ffmpegEXE) { super(); this.ffmpegEXE = ffmpegEXE; } /** * 视频添加背景音乐 * @param videoInputPath * @param mp3InputPath * @param seconds * @param videoOutputPath * @throws Exception */ public String convertor(String videoInputPath, String mp3InputPath, double seconds, String videoOutputPath) throws Exception { // 1. 获取视频中的音频 this.getBgmByVideo(videoInputPath); // 2. 合并bgm与视频中的音频 this.mergeBgm(mp3InputPath); // 3. 获取视频封面截图(我是因为所有资源都存储到了OSS上,把视频封面截图放到这里了) this.getCover(videoInputPath); // 4. 合并处理后的音频与视频 // ffmpeg.exe -i 苏州大裤衩.mp4 -i bgm.mp3 -t 7 -y -map 0:v:0 -map 1:a:0 新的视频.mp4 List<String> command = new ArrayList<>(); command.add(ffmpegEXE); command.add("-i"); command.add(videoInputPath); command.add("-i"); command.add(tempBgmPath); command.add("-t"); command.add(String.valueOf(seconds)); command.add("-y"); // 覆盖原有视频中的音频 command.add("-map"); command.add("0:v:0"); command.add("-map"); command.add("1:a:0"); command.add(videoOutputPath); this.processed(command); // 因为我要把图片存到OSS上,所以这里需要一个地址 return tempCoverPath; } /** * 获取视频中的音频 * @param videoPath * @return */ public boolean getBgmByVideo(String videoPath){ // ffmpeg -i input.mp4 -vn -y -acodec copy output.m4a try { List<String> command = new ArrayList<>(); command.add(ffmpegEXE); command.add("-i"); command.add(videoPath); command.add("-vn"); command.add("-y"); command.add("-acodec"); command.add("copy"); command.add(tempAudioPath); this.processed(command); return true; }catch (Exception e){ e.printStackTrace(); return false; } } /** * 合并bgm与视频中的音频 * @param bgmPath * @return */ public boolean mergeBgm(String bgmPath){ // ffmpeg -i input1.mp3 -i input2.mp3 -filter_complex amerge -ac 2 -c:a libmp3lame -q:a 4 output.mp3 try { List<String> command = new ArrayList<>(); command.add(ffmpegEXE); command.add("-i"); command.add(tempAudioPath); command.add("-i"); command.add(bgmPath); command.add("-filter_complex"); command.add("amerge"); command.add("-ac"); command.add("2"); command.add("-c:a"); command.add("libmp3lame"); command.add("-q:a"); command.add("4"); command.add("-y"); command.add(tempBgmPath); this.processed(command); return true; }catch (Exception e){ e.printStackTrace(); return false; } } /** * 获取视频封面截图 * @param videoInputPath * @return */ public String getCover(String videoInputPath) { // ffmpeg.exe -ss 00:00:01 -i spring.mp4 -vframes 1 bb.jpg try { List<String> command = new ArrayList<>(); command.add(ffmpegEXE); // 指定截取第1秒 command.add("-ss"); command.add("00:00:01"); command.add("-y"); command.add("-i"); command.add(videoInputPath); command.add("-vframes"); command.add("1"); command.add(tempCoverPath); this.processed(command); return tempCoverPath; }catch (Exception e){ e.printStackTrace(); return null; } } /** * 执行FFmpeg命令 * @param command * @throws IOException */ private void processed(List<String> command) throws IOException { ProcessBuilder builder = new ProcessBuilder(command); Process process = builder.start(); InputStream errorStream = process.getErrorStream(); InputStreamReader inputStreamReader = new InputStreamReader(errorStream); BufferedReader br = new BufferedReader(inputStreamReader); String line = ""; while ( (line = br.readLine()) != null ) { } if (br != null) { br.close(); } if (inputStreamReader != null) { inputStreamReader.close(); } if (errorStream != null) { errorStream.close(); } } }
012020-03-27 -
风间影月
2020-03-20
后续有空我更新一个视频吧,你先跳过好了
022020-03-21
相似问题