GPUImage Merge Videos with Chroma Key - GPUImageMovieWriter Two Audio Tracks
Feb 16, 2014
Let’s continue second topic about Merging videos with GPUImage: Writing both audio tracks of source videos to final output video.
So you want to write audio track of one of two source video to destination video, above code would works quite well.
For example, in above code we want to have audio of gpuMovieA video show up in final video. Then sometimes we found
that we need to merge two audio tracks in each source video into final output video.
I gave up and wrote my own code. GPUImage is great but it has so many issues and the code is not very easy to read for openGL noobs like me.
I learned more by writing it myself - all roads lead to rome, and all of GPUImage is basically based on a few examples out there on the net anyhow..
Well, Brad Larson has just commented to that request:
There currently is no way to do this kind of audio mixing. Only one audio source is used at a time.
#Problem Breakdown
Merging video tracks does work but audio doens’t is because the way how it works. For video tracks merging, there are two GPUImageMovie instances and each starts reading frames, uploading image buffer to GPU then grabbing the reference to rendered texture, passing to GPUImageMovieWriter instance. Then GPUImageMovieWriter bind references of two textures to chroma key shader’s input sample textures, rendered it. Finally grabbing the rendered output texture and write it to output video.
However audio tracks works quite differently, as you couldn’t mix two CMSampleBufferRef of audio track output. How about in the GPUImageMovieWriter we use AVMutableComposition to mix two audio tracks and output it as a source for asset writer to write it to final output video.