这个问题与AVMutableComposition - 视频资产之间的空白/黑框相关,但由于我没有使用AVAssetExportSession,因此答案不适合我的问题.
我正在使用AVMutableComposition来创建视频合成,我正在使用AVAssetReader读取它(我需要有帧数据,我不能使用AVPlayer)但我的视频块之间经常有黑框(有音频中没有明显的故障).
我创作我的作品
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
NSMutableArray* durationList = [NSMutableArray array];
NSMutableArray* videoList= [NSMutableArray array];
NSMutableArray* audioList= [NSMutableArray array];
for (NSInteger i = 0; i < [clips count]; i++)
{
AVURLAsset *myasset = [clips objectAtIndex:i];
AVAssetTrack *clipVideoTrack = [[myasset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[videoList addObject:clipVideoTrack];
AVAssetTrack *clipAudioTrack = [[myasset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[audioList addObject:clipAudioTrack];
CMTime clipDuration = [myasset duration];
CMTimeRange clipRange = CMTimeRangeMake(kCMTimeZero, clipDuration);
[durationList addObject:[NSValue valueWithCMTimeRange:clipRange]];
}
[compositionVideoTrack insertTimeRanges:durationList …Run Code Online (Sandbox Code Playgroud) 要求:
听起来有点不同,但这就是我想要实现的目标.我想反过来制作电影(.mov)文件.就像我们如何回放电影文件一样.我还希望保持与我的视频包含相同的帧速率.
注意:我不只是想以相反的顺序播放视频文件.我想以相反的顺序生成新的电影文件.
我的探索:
我想到了以下步骤来执行相同的操作.
AVAssetExportSessionAVMutableComposition和将所有这些视频块合并为单个电影文件AVAssetExportSession.使用上述步骤,我能够反过来实现生成的视频文件,但我有以下问题.
有没有人有任何其他优化的方法来实现这一目标?任何建议将不胜感激.
我正在使用AVMutableComposition混合音频和视频文件.以下是我的代码:
enter code here
AVMutableComposition* mixComposition = [AVMutableComposition composition];
NSString *bundleDirectory = [[NSBundle mainBundle] bundlePath];
NSString *audio_inputFilePath = [bundleDirectory stringByAppendingPathComponent:@"xyz.mp3"];//audio of 35 seconds
NSURL *audio_inputFileUrl = [NSURL fileURLWithPath:audio_inputFilePath];
NSURL *video_inputFileUrl = [NSURL fileURLWithPath:videoOutputPath];
NSString *outputFilePath = [documentsDirectory stringByAppendingPathComponent:@"video.mp4"];//video of 60 seconds
NSURL *outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath])
[[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];
CMTime nextClipStartTime = kCMTimeZero;
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:video_inputFileUrl options:nil];
CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime …Run Code Online (Sandbox Code Playgroud) 我有一个CALayer在AVMutableComposition被褪,应该在屏幕上停留了一会儿,然后消失.问题是,它应该在没有动画的情况下消失,但CABasicAnimation最小持续时间为0.25秒.
如何在没有动画的情况下在给定时间后设置图层的不透明度?
我试图反过来制作视频.在AVPlayer中播放资源时,我设置rate = -1以使其以反向格式工作.但如何导出该视频?我查看了文档.阅读有关avassetwrite,sambuffers,作品,但没有找到任何方法来做到这一点.以下链接由我提供
http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios
反向视频演示 - https://github.com/mikaelhellqvist/ReverseClip
以上示例不再适用于IOS 8.甚至它不会反转音频.如果有人给我一些暗示,那么我可以做进一步.我在过去的5天里被困在这里但在文档中没有找到任何答案.
video avfoundation avassetwriter avmutablecomposition avassetreader
我正在尝试使用AVMutableComposition在精确的时间播放一系列声音文件.
当视图加载时,我创建合成,目的是在1秒内均匀地播放4个声音.声音的长短不应该无关紧要,我只想在0,0.25,0.5和0.75秒时将它们发射:
AVMutableComposition *composition = [[AVMutableComposition alloc] init];
NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey : @YES};
for (NSInteger i = 0; i < 4; i++)
{
AVMutableCompositionTrack* track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
NSURL *url = [[NSBundle mainBundle] URLForResource:[NSString stringWithFormat:@"sound_file_%i", i] withExtension:@"caf"];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:options];
AVAssetTrack *assetTrack = [asset tracksWithMediaType:AVMediaTypeAudio].firstObject;
CMTimeRange timeRange = [assetTrack timeRange];
Float64 t = i * 0.25;
NSError *error;
BOOL success = [track insertTimeRange:timeRange ofTrack:assetTrack atTime:CMTimeMakeWithSeconds(t, 1) error:&error];
if (!success)
{
NSLog(@"unsuccesful creation of composition");
} …Run Code Online (Sandbox Code Playgroud) core-audio avfoundation ios avcomposition avmutablecomposition
我正在尝试使用合并多个视频AVMutableComposition.我面临的问题是每当我尝试添加AVMutableVideoComposition任何指令时,我的播放都会在AVPlayer6秒的时间内冻结.
另一个有趣的事情是,如果我在使用相同的导出后在iPad的照片应用程序中播放它就可以正常播放videoComposition.那为什么它会AVPlayer在6秒内冻结?
码:
AVMutableComposition *mutableComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *videoCompositionTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *audioCompositionTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeAudio
preferredTrackID:kCMPersistentTrackID_Invalid];
for (AVURLAsset *asset in assets)
{
AVAssetTrack *assetTrack;
assetTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetTrack *audioAssetTrack = [asset tracksWithMediaType:AVMediaTypeAudio].firstObject;
NSError *error;
[videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, assetTrack.timeRange.duration )
ofTrack:assetTrack
atTime:time
error:&error];
if (error) {
NSLog(@"asset url :: %@",assetTrack.asset);
NSLog(@"Error1 - %@", error.debugDescription);
}
[audioCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAssetTrack.timeRange.duration)
ofTrack:audioAssetTrack
atTime:time
error:&error];
if (error) {
NSLog(@"Error2 - …Run Code Online (Sandbox Code Playgroud) 我正在开发将多个视频与背景音轨结合在一起的应用程序.它还需要为不同的视频设置不同的音频级别.
以下是AssetItem类和AssetManager类的代码
// AssetItem Class
class AssetItem : NSObject {
var asset : Asset!
var assetEffect : AssetEffectType! // Enum
var assetSceneType : SceneType! // Enum
var videoLength : CMTime!
var animationLayer : AnyObject?
var volumeOfVideoVoice : Float = 0.0
var volumeOfBGMusic : Float = 0.0
override init() {
super.init()
}
}
// AssetManager Class implementation
class AssetManager{
var assetList = [AssetItem]()
var composition : AVMutableComposition! = AVMutableComposition()
var videoComposition : AVMutableVideoComposition? = AVMutableVideoComposition()
var audioMix : AVMutableAudioMix = AVMutableAudioMix() …Run Code Online (Sandbox Code Playgroud) 我想将 AVAsset- 合并arrayVideos为一个视频并将其保存在相机胶卷中。Raywenderlich.com 有一个很棒的教程,其中两个视频合并为一个。我创建了以下代码,但是导出到相机胶卷后获得的视频仅包含数组中的第一个和最后一个视频(不包括中间的其余视频arrayVideos)。我在这里错过了什么吗?
var arrayVideos = [AVAsset]() //Videos Array
var atTimeM: CMTime = CMTimeMake(0, 0)
var lastAsset: AVAsset!
var layerInstructionsArray = [AVVideoCompositionLayerInstruction]()
var completeTrackDuration: CMTime = CMTimeMake(0, 1)
var videoSize: CGSize = CGSize(width: 0.0, height: 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 = lastAsset!.duration
}
try videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), of: videoAsset.tracks(withMediaType: …Run Code Online (Sandbox Code Playgroud) 导出后我AVMutableComposition使用PHPhotoLibrary将视频保存到相机胶卷.在creationRequestForAssetFromVideoAtFileURL:完成处理程序中,然后我在Instagram中打开已保存的视频,如下所示:
__block PHObjectPlaceholder *videoAssetPlaceholder;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:localVideoURL];
videoAssetPlaceholder = req.placeholderForCreatedAsset;
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
completion(YES);
NSString *localID = videoAssetPlaceholder.localIdentifier;
NSRange rangeOfSlash = [localID rangeOfString:@"/"];
if (rangeOfSlash.location != NSNotFound) {
NSString *assetID = [localID substringToIndex:rangeOfSlash.location];
NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://library?AssetPath=%@", assetID]];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
}
}
}
}];
Run Code Online (Sandbox Code Playgroud)
Instagram打开的时间大约有50%,视频播放时间与预期相符.然而,另外50%的时间,视频和预览都是白色的,而我得到的只是声音.这通常通过选择另一个视频然后返回到我的视频来解决.视频在相机胶卷中完美播放,只有Instagram会导致问题.这是Instagram有问题还是我可能以错误的方式导出我的视频的问题?
这些是我的AVAssetExportSession设置:
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
presetName:AVAssetExportPresetHighestQuality]; …Run Code Online (Sandbox Code Playgroud) ios avmutablecomposition instagram avassetexportsession instagram-api
ios ×7
avfoundation ×5
video ×4
avasset ×2
arrays ×1
calayer ×1
core-audio ×1
instagram ×1
iphone ×1
objective-c ×1
swift ×1