我有这段代码可以将视频的大小从 1280 x 720 调整为 640 x 360 但我想要一个不裁剪的调整大小。
有没有办法完全调整不裁剪的大小?
这是代码
class func resizer(inputURL : NSURL , completion: (outPutURL : NSURL?) -> Void ){
let videoAsset = AVAsset(URL: inputURL) as AVAsset
let clipVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo).first! as AVAssetTrack
let composition = AVMutableComposition()
composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())
let videoComposition = AVMutableVideoComposition()
videoComposition.renderSize = CGSizeMake(360,640)
videoComposition.frameDuration = CMTimeMake(1, 30)
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(180, 30))
let transformer : AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
let t1 = CGAffineTransformMakeTranslation(360, 0)
let t2 = CGAffineTransformRotate(t1, …Run Code Online (Sandbox Code Playgroud) 我正在使用ARVideoKit来录制屏幕(ReplayKit 对此不起作用),有时我可以毫无问题地录制和保存。其他时候我记录,当我去保存时,我会崩溃:
** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“*** -[AVAssetWriterInput appendSampleBuffer:] 无法追加示例缓冲区:必须先启动会话(使用 -[AVAssetWriter startSessionAtSourceTime:)”
查看 StackTrace 它是一个__pthread__kill,它发生在thread 83:
具体在此DispatchQueue:
let audioBufferQueue = DispatchQueue(label: "com.ahmedbekhit.AudioBufferQueue")
我怎样才能防止这种情况发生?
这是文件中的代码:
import AVFoundation
import CoreImage
import UIKit
@available(iOS 11.0, *)
class WritAR: NSObject, AVCaptureAudioDataOutputSampleBufferDelegate {
private var assetWriter: AVAssetWriter!
private var videoInput: AVAssetWriterInput!
private var audioInput: AVAssetWriterInput!
private var session: AVCaptureSession!
private var pixelBufferInput: AVAssetWriterInputPixelBufferAdaptor!
private var videoOutputSettings: Dictionary<String, AnyObject>!
private var audioSettings: [String: Any]?
let audioBufferQueue = DispatchQueue(label: "com.ahmedbekhit.AudioBufferQueue")
private var …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 iPhone 的前置摄像头录制有声视频。由于我还需要支持暂停/恢复功能,因此我需要使用AVAssetWriter. 我在网上找到了一个用 Objective-C 编写的示例,它几乎实现了所需的功能(http://www.gdcl.co.uk/2013/02/20/iPhone-Pause.html)
不幸的是,在将此示例转换为 Swift 后,我注意到如果我暂停/恢复,在每个“部分”的末尾都会有一个小但明显的时间段,在此期间视频只是一个静止帧并且正在播放音频。所以,似乎当isPaused触发时,录制的音轨比录制的视频轨长。
抱歉,如果这看起来像是一个菜鸟问题,但我不是这方面的专家,AVFoundation如果能提供一些帮助,我们将不胜感激!
下面我发布我的实现didOutput sampleBuffer。
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
var isVideo = true
if videoConntection != connection {
isVideo = false
}
if (!isCapturing || isPaused) {
return
}
if (encoder == nil) {
if isVideo {
return
}
if let fmt = CMSampleBufferGetFormatDescription(sampleBuffer) {
let desc = CMAudioFormatDescriptionGetStreamBasicDescription(fmt as CMAudioFormatDescription)
if let chan = desc?.pointee.mChannelsPerFrame, …Run Code Online (Sandbox Code Playgroud) 嘿我正在尝试记录我的游戏的游戏,以便我可以从设备本身上传其视频到youtube ...我试图做同样的事情,如iPhone的会说话的tomcat应用程序..记录视频然后播放,等等...
我使用glReadPixels()来读取帧缓冲数据,然后在AVFoundation框架中借助AVAssetWriter将其写入视频.但是,在使用glReadPixels时,读取每个图形上的数据会将FPS从大约30-35降低到2-3.
我认为会说话的tomcat也是在Opengl ES的帮助下制作的,它也有视频录制功能,但在阅读每一帧任何想法时都不会减慢速度....?
在我的iOS应用中,我需要将图像保存为短视频片段.我有这个工作使用AVAssetWriter和AVAssetWriterPixelBufferAdaptor,感谢这个网站上的一些很棒的帖子,但我不得不捏造开始和结束会话时间,以及演示时间,因为我真的不了解它们.
以下片段创建了一个2秒的视频,但我通过反复试验设置了不同的时间.老实说,我不确定为什么它不能制作3秒的视频.
// start session
videoWriter.movieFragmentInterval = CMTimeMake(1,600);
[videoWriter startWriting];
CMTime startTime = CMTimeMake(0, 600);
[videoWriter startSessionAtSourceTime:startTime];
while (1) {
if (![writerInput isReadyForMoreMediaData]) {
NSLog(@"Not ready for data");
} else {
[avAdaptor appendPixelBuffer:pixelBuffer
withPresentationTime:CMTimeMake(1200,600)];
break;
}
}
//Finish the session:
[writerInput markAsFinished];
CMTime endTime = CMTimeMake(1800, 600);
[videoWriter endSessionAtSourceTime:endTime];
[videoWriter finishWriting];
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释这个片段中的各种时间设置,或者指向一个有用的文档吗?我读过苹果文档,直到我睁大眼睛,但我猜它们比我现在拥有更多的知识.
TIA:约翰
我正在使用AVAssetWriter和captureOutput回调来录制视频和音频。问题在于,在大约5%的情况下,我无法从第二个零开始生成缩略图(如果我尝试在视频中走得更远,则没有问题)。我得到的错误是:
错误域= AVFoundationErrorDomain代码= -11832“无法打开” UserInfo = 0x4b31b0 {NSLocalizedFailureReason =无法使用该介质。NSUnderlyingError = 0x4effb0“操作无法完成。(OSStatus错误-12431。)” }
这是我正在使用的代码:
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
[asset release];
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error)
{
if (result != AVAssetImageGeneratorSucceeded)
{
[self NSLogPrint:[NSString stringWithFormat:@"couldn't generate thumbnail, error:%@", error]];
}
UIImage *thumbImg=[[UIImage imageWithCGImage:im] retain];
[image setImage:thumbImg];
[thumbImg release];
[generator release];
};
CGSize maxSize = CGSizeMake(177, 100);
generator.maximumSize = …Run Code Online (Sandbox Code Playgroud) 我正在使用一些技巧来尝试在写入磁盘时读取AVAssetWriter的原始输出.当我通过连接它们重新组装单个文件时,生成的文件与AVAssetWriter的输出文件具有相同的确切字节数.但是,重新组装的文件将无法在QuickTime中播放或由FFmpeg解析,因为存在数据损坏.这里和那里的几个字节已经改变,使得结果文件无法使用.我假设这发生在每次读取的EOF边界上,但它不是一致的损坏.
我计划最终使用与此类似的代码从编码器中解析出单个H.264 NAL单元以将它们打包并通过RTP发送它们,但是如果我不相信从磁盘读取的数据,我可能不得不使用另一种解决方案.
是否有解释/修复此数据损坏?您是否有任何其他资源/链接可以解析如何解析NAL单元以通过RTP进行打包?
完整代码:AVAppleEncoder.m
// Modified from
// http://www.davidhamrick.com/2011/10/13/Monitoring-Files-With-GCD-Being-Edited-With-A-Text-Editor.html
- (void)watchOutputFileHandle
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
int fildes = open([[movieURL path] UTF8String], O_EVTONLY);
source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE,fildes,
DISPATCH_VNODE_DELETE | DISPATCH_VNODE_WRITE | DISPATCH_VNODE_EXTEND | DISPATCH_VNODE_ATTRIB | DISPATCH_VNODE_LINK | DISPATCH_VNODE_RENAME | DISPATCH_VNODE_REVOKE,
queue);
dispatch_source_set_event_handler(source, ^
{
unsigned long flags = dispatch_source_get_data(source);
if(flags & DISPATCH_VNODE_DELETE)
{
dispatch_source_cancel(source);
//[blockSelf watchStyleSheet:path];
}
if(flags & DISPATCH_VNODE_EXTEND)
{
//NSLog(@"File size changed");
NSError *error = nil;
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingFromURL:movieURL error:&error];
if (error) {
[self …Run Code Online (Sandbox Code Playgroud) objective-c nsdata h.264 grand-central-dispatch avassetwriter
我想在使用AVAssetWriter和的图像数组创建的视频中添加淡入/淡出,翻转和页面卷曲过渡效果AVAssetComposition.
我看到"在AV基金会中与媒体合作"从18:00左右开始谈论这个问题,但它显示了两个视频之间的淡入淡出效果.我希望单个视频中的每个图像之间的淡入淡出效果.并且还想要翻转和卷曲效果.
我不得不将大文件大小的歌曲从iTunes库转换为较小的8K歌曲文件.
当我执行转换异步时,即使写入doc文件夹未完成,bool也总是返回true.目前我正在使用10秒的延迟再次调用该功能,它在iPhone 5s的临时工作正常,但我想迎合较慢的设备.
请在我的代码上给我一些指针/推荐.
-(void)startUploadSongAnalysis
{
[self updateProgressYForID3NForUpload:NO];
if ([self.uploadWorkingAray count]>=1)
{
Song *songVar = [self.uploadWorkingAray objectAtIndex:0];//core data var
NSLog(@"songVar %@",songVar.songName);
NSLog(@"songVar %@",songVar.songURL);
NSURL *songU = [NSURL URLWithString:songVar.songURL]; //URL of iTunes Lib
// self.asset = [AVAsset assetWithURL:songU];
// NSLog(@"asset %@",self.asset);
NSError *error;
NSString *subString = [[songVar.songURL componentsSeparatedByString:@"id="] lastObject];
NSString *savedPath = [self.documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"audio%@.m4a",subString]];//save file name of converted 8kb song
NSString *subStringPath = [NSString stringWithFormat:@"audio%@.m4a",subString];
if ([self.fileManager fileExistsAtPath:savedPath] == YES)
[self.fileManager removeItemAtPath:savedPath error:&error];
NSLog(@"cacheDir %@",savedPath);
//export low bitrate song to …Run Code Online (Sandbox Code Playgroud) objective-c nsdocument ios avassetwriter avassetexportsession
我正在尝试使用AVAsset和AVAssetWriter在iOS中反转音频.以下代码正常,但输出文件比输入短.例如,输入文件的持续时间为1:59,但输出1:50且音频内容相同.
- (void)reverse:(AVAsset *)asset
{
AVAssetReader* reader = [[AVAssetReader alloc] initWithAsset:asset error:nil];
AVAssetTrack* audioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
NSMutableDictionary* audioReadSettings = [NSMutableDictionary dictionary];
[audioReadSettings setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM]
forKey:AVFormatIDKey];
AVAssetReaderTrackOutput* readerOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:audioTrack outputSettings:audioReadSettings];
[reader addOutput:readerOutput];
[reader startReading];
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
[NSNumber numberWithFloat:44100.0], AVSampleRateKey,
[NSNumber numberWithInt:2], AVNumberOfChannelsKey,
[NSNumber numberWithInt:128000], AVEncoderBitRateKey,
[NSData data], AVChannelLayoutKey,
nil];
AVAssetWriterInput *writerInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeAudio
outputSettings:outputSettings];
NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"out.m4a"];
NSURL *exportURL = [NSURL fileURLWithPath:exportPath];
NSError *writerError = nil; …Run Code Online (Sandbox Code Playgroud) avassetwriter ×10
ios ×7
avfoundation ×4
iphone ×3
objective-c ×3
swift ×3
video ×2
avasset ×1
core-audio ×1
glreadpixels ×1
h.264 ×1
nsdata ×1
nsdocument ×1
opengl-es ×1