请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

老师为什么我上传抖音的视频然后搭上自己的bgm后生成的新视频没有效果呢

要合并的bgm并没有在新视频中播放出来,依旧是原来抖音的原声。而且我一下子生成了两个视频图片描述

正在回答 回答被采纳积分+3

3回答

六维世界 2020-03-26 13:21:02

我也是同样的问题,添加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


2 回复 有任何疑惑可以回复我~
  • 提问者 寿喜烧爱好者 #1
    老哥能给一下你的MergeVideoMP3工具类的代码嘛
    回复 有任何疑惑可以回复我~ 2020-03-26 21:05:53
  • 求老哥这部分的代码,在这死活进行不下去了
    回复 有任何疑惑可以回复我~ 2020-03-27 00:08:25
  • 六维世界 回复 提问者 寿喜烧爱好者 #3
    好的,方便大家查看,又重新回答了一次,你可以参考一下
    回复 有任何疑惑可以回复我~ 2020-03-27 10:37:26
六维世界 2020-03-27 10:56:42

// 根据自己情况改动,我这里的代码可以当做一个参考

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();
     }
  }
}


0 回复 有任何疑惑可以回复我~
  • 提问者 寿喜烧爱好者 #1
    ok,谢谢老铁,我参考下。
    回复 有任何疑惑可以回复我~ 2020-03-27 23:14:52
风间影月 2020-03-20 18:47:14

后续有空我更新一个视频吧,你先跳过好了

0 回复 有任何疑惑可以回复我~
  • 提问者 寿喜烧爱好者 #1
    嗯嗯,好的老师
    回复 有任何疑惑可以回复我~ 2020-03-20 22:49:47
  • 提问者 寿喜烧爱好者 #2
    老师我一开始以为新合成的视频没有bgm是因为bgm的路径配错了,但是我改回来后还是一样没声音。至于文件夹下有俩视频,一个是原来的,一个是新合成的。我现在的问题就是我的ffmpeg合成的视频没有声音,谢谢老师指点我了。
    回复 有任何疑惑可以回复我~ 2020-03-21 00:11:35
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信