如何合并两个透明的视频
how to merge two video with transparency
我已经使用 AVFoundation 框架 成功合并视频 1 和视频 2,视频 2 是透明的,但是在下面合并视频(视频 1)之后是不显示只有视频 2 可见但是当我使用下面的代码时
AVMutableVideoCompositionLayerInstruction *SecondlayerInstruction =[AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:secondTrack];
[SecondlayerInstruction setOpacity:0.6 atTime:kCMTimeZero];
它在 video-2 上设置了不透明度 layer.But 这里的实际问题是,video-2 层上有一些内容不透明,在对 video-2 层应用不透明度后,它也适用于该层内容不透明
我在这里添加了两张图片,描述了使用 AVMutableVideoCompositionLayerInstruction
设置不透明度后的两种情况
- 在图像中合并透明区域后是黑色的,当我在整个第二层设置不透明度时,视频 2 现在变得透明,但内容也变得透明。
- 但我的问题是如何在 merging.I 已经检查视频 2 是透明的后如何在另一个视频上播放透明视频,因为它在 android 平台上可以正常播放。
Edited-1 : 我也尝试在 myVideoCompositionInstruction
上设置背景颜色,但这也没有帮助。参考这个老问题 link
Edited-2 : 在 AVVideoComposition.h 中,我发现
Indicates the background color of the composition. Solid BGRA colors
only are supported; patterns and other color refs that are not
supported will be ignored. If the background color is not specified
the video compositor will use a default backgroundColor of opaque
black. If the rendered pixel buffer does not have alpha, the alpha
value of the backgroundColor will be ignored.
这是什么意思,我没有得到it.can任何人的帮助?
您可以设置视频的 alpha 而不是不透明度。
解释:
Alpha
设置元素及其所有子元素的不透明度值,而 opacity
仅为单个组件设置不透明度值。
好问题:
试试这个
var totalTime : CMTime = CMTimeMake(0, 0)
func mergeVideoArray() {
let mixComposition = AVMutableComposition()
for videoAsset in arrayVideos {
let videoTrack =
mixComposition.addMutableTrack(withMediaType: AVMediaTypeVideo,
preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
do {
if videoAsset == arrayVideos.first {
atTimeM = kCMTimeZero
} else {
atTimeM = totalTime // <-- Use the total time for all the videos seen so far.
}
try videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration),
of: videoAsset.tracks(withMediaType: AVMediaTypeVideo)[0],
at: atTimeM)
videoSize = videoTrack.naturalSize
} catch let error as NSError {
print("error: \(error)")
}
totalTime += videoAsset.duration // <-- Update the total time for all videos.
...
这对我有用。我把第一个视频放在第二个视频上面。我希望第一个视频的不透明度为 0.7
let firstVideoCompositionTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
let secondVideoCompositionTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
// ... run MixComposition ...
let firstLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: firstVideoCompositionTrack!)
firstLayerInstruction.setOpacity(0.7, at: .zero) <-----HERE
// rest of code
我已经使用 AVFoundation 框架 成功合并视频 1 和视频 2,视频 2 是透明的,但是在下面合并视频(视频 1)之后是不显示只有视频 2 可见但是当我使用下面的代码时
AVMutableVideoCompositionLayerInstruction *SecondlayerInstruction =[AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:secondTrack];
[SecondlayerInstruction setOpacity:0.6 atTime:kCMTimeZero];
它在 video-2 上设置了不透明度 layer.But 这里的实际问题是,video-2 层上有一些内容不透明,在对 video-2 层应用不透明度后,它也适用于该层内容不透明
我在这里添加了两张图片,描述了使用 AVMutableVideoCompositionLayerInstruction
- 在图像中合并透明区域后是黑色的,当我在整个第二层设置不透明度时,视频 2 现在变得透明,但内容也变得透明。
- 但我的问题是如何在 merging.I 已经检查视频 2 是透明的后如何在另一个视频上播放透明视频,因为它在 android 平台上可以正常播放。
Edited-1 : 我也尝试在 myVideoCompositionInstruction
上设置背景颜色,但这也没有帮助。参考这个老问题 link
Edited-2 : 在 AVVideoComposition.h 中,我发现
Indicates the background color of the composition. Solid BGRA colors only are supported; patterns and other color refs that are not supported will be ignored. If the background color is not specified the video compositor will use a default backgroundColor of opaque black. If the rendered pixel buffer does not have alpha, the alpha value of the backgroundColor will be ignored.
这是什么意思,我没有得到it.can任何人的帮助?
您可以设置视频的 alpha 而不是不透明度。
解释:
Alpha
设置元素及其所有子元素的不透明度值,而 opacity
仅为单个组件设置不透明度值。
好问题:
试试这个
var totalTime : CMTime = CMTimeMake(0, 0)
func mergeVideoArray() {
let mixComposition = AVMutableComposition()
for videoAsset in arrayVideos {
let videoTrack =
mixComposition.addMutableTrack(withMediaType: AVMediaTypeVideo,
preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
do {
if videoAsset == arrayVideos.first {
atTimeM = kCMTimeZero
} else {
atTimeM = totalTime // <-- Use the total time for all the videos seen so far.
}
try videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration),
of: videoAsset.tracks(withMediaType: AVMediaTypeVideo)[0],
at: atTimeM)
videoSize = videoTrack.naturalSize
} catch let error as NSError {
print("error: \(error)")
}
totalTime += videoAsset.duration // <-- Update the total time for all videos.
...
这对我有用。我把第一个视频放在第二个视频上面。我希望第一个视频的不透明度为 0.7
let firstVideoCompositionTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
let secondVideoCompositionTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
// ... run MixComposition ...
let firstLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: firstVideoCompositionTrack!)
firstLayerInstruction.setOpacity(0.7, at: .zero) <-----HERE
// rest of code