标签: avmutablecomposition

设置AVMutableComposition的frameDuration

我正在使用AVEditDemoApple的WWDC 2010样本包中的项目,我正在尝试更改导出视频的帧速率.使用AVMutableCompositionframeDuration设置的视频导出视频:

videoComposition.frameDuration = CMTimeMake(1, 30); // 30 fps
Run Code Online (Sandbox Code Playgroud)

出于某种原因,将30更改为25并不会更改使用framerate该视频导出的视频AVAssetExportSession.有谁知道为什么?

iphone avfoundation ios avmutablecomposition avassetexportsession

5
推荐指数
3
解决办法
4142
查看次数

iOS:将空/黑帧添加到AVMutableComposition

从CAF录音开始,我需要创建一个视频文件,其中视频轨道只是空帧,音轨是这个录音文件.我知道这是一个奇怪的用例,这可能就是为什么我找不到有关如何操作的信息.我正在尝试使用AVMutableComposition,但我无法弄清楚如何让它工作.

这就是我现在所拥有的:

AVMutableComposition *composition = [AVMutableComposition composition];    

/* soundFilePath is the path to the CAF audio file */
AVURLAsset *audioAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:soundFilePath] options:nil];
CMTimeRange fullTime = CMTimeRangeMake(kCMTimeZero, [audioAsset duration]);
AVAssetTrack *sourceAudioTrack = [[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

/* Add audio track */
AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack insertTimeRange:fullTime ofTrack:sourceAudioTrack atTime:kCMTimeZero error:nil];

/* docsDir is defined previously as the path to where I want to save the file */
NSString *videoFilePath = [docsDir stringByAppendingPathComponent:@"video.mov"];
NSURL *videoFileURL …
Run Code Online (Sandbox Code Playgroud)

iphone avfoundation ios avmutablecomposition

5
推荐指数
0
解决办法
2103
查看次数

将AVMutableVideoComposition的文本叠加添加到特定时间范围

我找到了一些示例,说明如何在视频上添加文本叠加层.

Ray的教程 - http://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos

这个答案 - 如何在iOS上捕获的视频中添加水印

我还引用了其他几个.我的代码看起来与答案几乎完全相同,我一直在尝试调整它以使文本覆盖仅在视频开头显示一两秒钟.有关如何实现这一目标的任何帮助?

这是我的代码.这样可以导出视频,并在整个持续时间内显示叠加层.

if(vA) {
    videoCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    [videoCompositionTrack insertTimeRange:videoTimerange ofTrack:[[vA tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:&error];
    [videoCompositionTrack setPreferredTransform:[[[vA tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]];
    if(error)
        NSLog(@"%@", error);
}

if(aA) {
    audioCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    [audioCompositionTrack insertTimeRange:audioTimerange ofTrack:[[aA tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:&error];
    if(error)
        NSLog(@"%@", error);
}

CGSize videoSize = [videoCompositionTrack naturalSize];

UIImage *myImage = [UIImage imageNamed:@"cover.png"];
CALayer *aLayer = [CALayer layer];
aLayer.contents = (id)myImage.CGImage;
aLayer.frame = CGRectMake(5, 25, 100, 56);
aLayer.opacity = 0.7;

CATextLayer *titleLayer = …
Run Code Online (Sandbox Code Playgroud)

xcode avfoundation ios avmutablecomposition

5
推荐指数
2
解决办法
5891
查看次数

如何在AVMutableComposition中同时为多个视频层设置动画?

我正在编写一段代码,以从iOS设备上的多个图像和多个视频生成幻灯片视频。我可以用一个视频和多个图像来做到这一点,但是我不知道如何将其增强为多个视频。

这是我可以用一个视频和两个图像生成的示例视频

这是准备出口商的主要程序。

// Prepare the temporary location to store generated video
NSURL * urlAsset = [NSURL fileURLWithPath:[StoryMaker tempFilePath:@"mov"]];

// Prepare composition and _exporter
AVMutableComposition *composition = [AVMutableComposition composition];
AVAssetExportSession* exporter = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL = urlAsset;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
exporter.videoComposition = [self _addVideo:composition time:timeVideo];
Run Code Online (Sandbox Code Playgroud)

这是_addVideo:time:方法,它创建videoLayer。

-(AVVideoComposition*) _addVideo:(AVMutableComposition*)composition time:(CMTime)timeVideo {
    AVMutableVideoComposition* videoComposition = [AVMutableVideoComposition videoComposition];
    videoComposition.renderSize = _sizeVideo;
    videoComposition.frameDuration = CMTimeMake(1,30); // 30fps
    AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,timeVideo) ofTrack:_baseVideoTrack atTime:kCMTimeZero …
Run Code Online (Sandbox Code Playgroud)

video core-animation avfoundation ios avmutablecomposition

5
推荐指数
1
解决办法
1302
查看次数

AVMutableVideoCompositionLayerInstruction转换比例旋转

我正在使用AVMutableComposition合并/缝合多个录制的视频轨道并将其保存为预期的输出

..所以我循环遍历子视图,根据每个子视图大小转换每个轨道.

    var index = 0
    for subview:UIView in template.subviews {
        let scaleX = (subview.frame.width / template.frame.width) / (tracks[index].naturalSize.width /  mainComposition.renderSize.width)
        let scaleY = (subview.frame.height / template.frame.height) / (tracks[index].naturalSize.height /  mainComposition.renderSize.height)
        let x = subview.frame.origin.x
        let y = subview.frame.origin.y

        let rotate = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
        let scale = CGAffineTransformMakeScale(scaleX, scaleY)
        let translate = CGAffineTransformMakeTranslation(x, y)
        let transform = CGAffineTransformConcat(scale, translate)

        let transformation = AVMutableVideoCompositionLayerInstruction(assetTrack: tracks[index])
        transformation.setTransform(CGAffineTransformConcat(rotate, transform), atTime: kCMTimeZero)
        instructions.append(transformation)
        index++
    }
Run Code Online (Sandbox Code Playgroud)

问题是视频全部不在保存的视频中.但是如果没有旋转,我会得到预期的输出,如附图中的视频,但视频中的视频 - 我读到的是iPhone设备中捕获的视频总是如此(如何通过AVAssetExportSession以纵向模式导出视频资源)

我一直在玩CGAffineTransform,重新排列翻译,旋转或缩放,这是首先或最后但没有得到好结果.我也试过创建两个分离旋转和sctaletranslate但仍然相同的layerinstruction.

谢谢你的帮助

avfoundation orientation avmutablecomposition swift

5
推荐指数
0
解决办法
534
查看次数

在IOS中沿水平轴翻转前置摄像头录制的视频

我正试图在录制视频时解除iPhone前置摄像头所产生的镜像效果.基本上,我想将前置摄像头沿水平轴拍摄的视频翻转,这样最终编辑过的视频就像内置的摄像头应用程序一样没有镜像效果.我正在使用AVCaptureSession录制视频.

// 3.1 - Create AVMutableVideoCompositionInstruction
    AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, self.videoAsset.duration);

// 3.2 - Create an AVMutableVideoCompositionLayerInstruction for the video track and fix the orientation.
AVMutableVideoCompositionLayerInstruction *videolayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
AVAssetTrack *videoAssetTrack = [[self.videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

UIImageOrientation videoAssetOrientation_  = UIImageOrientationUp;
BOOL isVideoAssetPortrait_  = NO;
CGAffineTransform videoTransform = videoAssetTrack.preferredTransform;
if (videoTransform.a == 0 && videoTransform.b == 1.0 && videoTransform.c == -1.0 && videoTransform.d == 0)
{
    videoAssetOrientation_ = UIImageOrientationRight;
    isVideoAssetPortrait_ = YES;
}
if (videoTransform.a == 0 …
Run Code Online (Sandbox Code Playgroud)

video-capture cgaffinetransform ios avmutablecomposition front-camera

4
推荐指数
1
解决办法
3663
查看次数

AVPlayer无法使用AirPlay

我有一系列AVMutableCompositions,我正在玩AVPlayer.当我通过扬声器,耳机或蓝牙播放时,一切都很棒.然而,一旦我连接到AirPlay,一切都崩溃了.

我可以玩一个资产,我尝试切换到新资产,我得到一个AVAudioSessionMediaServicesWereResetNotification,设备断开与AirPlay.我尝试将AVMutableCompositions导出到文件然后使用AVURLAssets,但这似乎没有改变任何东西.

有谁知道这里发生了什么?看起来我无法支持AirPlay ......

我觉得我可以切换到AVAudioPlayer,这将解决问题,但我生成了几十个AVMutableCompositions,不想读取和写入磁盘.

avfoundation ios avplayer avmutablecomposition airplay

4
推荐指数
1
解决办法
1624
查看次数

使用AVMutableComposition合并视频时视频破解

我正在使用AVMutableComposition将视频与下面的代码合并,

- (void)MergeAndSave_internal{

    AVMutableComposition *composition = [AVMutableComposition composition];
    AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
    videoComposition.frameDuration = CMTimeMake(1,30);
    videoComposition.renderScale = 1.0;

    AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];

    AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack];

    NSLog(@"%@",videoPathArray);

    float time = 0;
    CMTime startTime = kCMTimeZero;

    for (int i = 0; i<videoPathArray.count; i++) {

        AVURLAsset *sourceAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[videoPathArray objectAtIndex:i]] options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey]];

        NSError *error = nil;

        BOOL ok = NO;
        AVAssetTrack *sourceVideoTrack = …
Run Code Online (Sandbox Code Playgroud)

video objective-c ios avmutablecomposition avassetexportsession

4
推荐指数
1
解决办法
502
查看次数

AVMutableComposition 旋转视频文件

我正在使用 PryntTrimmerView 来修剪视频文件。有我的代码用于导出修剪视频文件并生成视频缩略图:

func prepareAssetComposition() throws {

    guard let asset = trimmerView.asset, let videoTrack = asset.tracks(withMediaType: AVMediaTypeVideo).last else {
      return
    }
    let size = videoTrack.naturalSize.applying(videoTrack.preferredTransform)
    print(CGSize(width: fabs(size.width), height: fabs(size.height)))

    let assetComposition = AVMutableComposition()
    let start = trimmerView.startTime?.seconds
    let end = trimmerView.endTime?.seconds
    let startTime = CMTime(seconds: Double(start ?? 0), preferredTimescale: 1000)
    let endTime = CMTime(seconds: Double(end ?? 0), preferredTimescale: 1000)
    let trackTimeRange = CMTimeRange(start: startTime, end: endTime)


    let compositionTrack = assetComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
    try compositionTrack.insertTimeRange(trackTimeRange, of: videoTrack, at: kCMTimeZero)

    var …
Run Code Online (Sandbox Code Playgroud)

trim ios avmutablecomposition avassetexportsession swift

4
推荐指数
1
解决办法
2159
查看次数

如何使用淡入淡出过渡合并 2 个视频?

我正在尝试将两个重叠的 2 个视频合并为 2 秒。在这种重叠中,我想淡入第二个视频(或淡出第一个视频以显示第二个视频,任何一个都很棒)。

第一个视频按预期在结束前 2 秒淡出,但当它淡出时,我看到的是黑屏,而不是第二个视频淡入。在视频 1 结束时,视频 2 在动画淡入过程中显示了一半。

我看不到重叠的轨道,我做错了什么?下面是我的代码

func setupVideo() {
        
    let url = URL(fileURLWithPath: Bundle.main.path(forResource: "demoVideoTwo", ofType: "mp4")!)
    let assetOne = AVAsset(url: url)
    
    let urlTwo = URL(fileURLWithPath: Bundle.main.path(forResource: "demoVideoThree", ofType: "mp4")!)
    let assetTwo = AVAsset(url: urlTwo)
            
    let mixComposition = AVMutableComposition()
    var instructions = [AVMutableVideoCompositionLayerInstruction]()
    var mainInstructionList = [AVMutableVideoCompositionInstruction]()
    var lastTime = CMTime.zero
    
    // Create Track One

    guard let videoTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: Int32(kCMPersistentTrackID_Invalid)), let audioTrack = mixComposition.addMutableTrack(withMediaType: .audio, preferredTrackID: Int32(kCMPersistentTrackID_Invalid)) else {
        return …
Run Code Online (Sandbox Code Playgroud)

avfoundation avplayer avmutablecomposition avasset swift

4
推荐指数
1
解决办法
1105
查看次数