合并两个有和没有音频的剪辑会导致音频在生成的视频中从头开始

Posted

技术标签:

【中文标题】合并两个有和没有音频的剪辑会导致音频在生成的视频中从头开始【英文标题】:Merging two clips with & without audio causes the audio to start from beginning in the generated video 【发布时间】:2020-01-23 06:13:37 【问题描述】:

我正在尝试使用 MP4Parser 将两个视频(mp4 文件)合并为一个视频,一个有音频,一个没有音频。生成的最终视频始终具有从头开始的音频。 例如第一个视频的长度为 20 秒,没有音频。第二个视频长度为 10 秒,有音频。 生成的最终视频长度为 30 秒,但音频将在视频开头播放 10 秒(其余 20 秒不会有任何音频),而它应该开始 20 秒没有音频,结束 10 秒有音频。

Movie[] clips = new Movie[2];

//location of the movie clip storage
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES), "TestMerge");

//Build the two clips into movies
Movie firstClip = MovieCreator.build(first);
Movie secondClip = MovieCreator.build(second);

//Add both movie clips
clips[0] = firstClip;
clips[1] = secondClip;

//List for audio and video tracks
List<Track> videoTracks = new LinkedList<Track>();
List<Track> audioTracks = new LinkedList<Track>();

//Iterate all the movie clips and find the audio and videos
for (Movie movie: clips) 
    for (Track track : movie.getTracks()) 
        if (track.getHandler().equals("soun")) 
            audioTracks.add(track);                
        if (track.getHandler().equals("vide"))
            videoTracks.add(track);
    


//Result movie from putting the audio and video together from the two clips
Movie result = new Movie();

//Append all audio and video
if (videoTracks.size() > 0)
    result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));

if (audioTracks.size() > 0) 
    result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));

我尝试通过更改以下几行来添加静音轨道

//List for audio and video tracks
List<Track> videoTracks = new LinkedList<Track>();
List<Track> audioTracks = new LinkedList<Track>();
Track audiosourceTrack = null;

    for (final Movie movie : inMovies) 
        for (final Track t : movie.getTracks()) 
            if (isAudioTrack(t)) 
                audioSourceTrack = t;
                break;
            
        
        if (audioSourceTrack != null) break;
    

    //Iterate all the movie clips and find the audio and videos
for (Movie movie: clips) 
        if (movie.getTracks().size() == 1) 
            videoTracks.add(movie.getTracks().get(0));
            audioTracks.add(new SilenceTrackImpl(audioSourceTrack, movie.getTracks().get(0).getDuration() / 2));
         else 
            for (final Track t : movie.getTracks()) 
                if (isAudioTrack(t)) 
                    audioTracks.add(t);
                
                if (isVideoTrack(t)) 
                    videoTracks.add(t);
                
            
        
    

【问题讨论】:

【参考方案1】:

我可以通过将静音剪辑附加静音轨道来做到这一点。问题是 react-native-video 没有像 Q 播放器那样正确播放静音轨道。我必须对视频进行转码才能看到附加的静音是否正常工作。

【讨论】:

以上是关于合并两个有和没有音频的剪辑会导致音频在生成的视频中从头开始的主要内容,如果未能解决你的问题,请参考以下文章

在moviepy中组合音频剪辑时的音频帧重复

音频合并,剪切,降采样,时长等处理

iOS - 将两个视频合并为一个带音频的视频文件

python实战项目惊呆了,竟然能剪辑视频!

请问剪辑WAV文件的软件哪个好?

如何在ffmpeg中合并音频和两个视频文件?