我有一个严重的问题:我有NSArray几个UIImage对象.我现在想做的是从那些创作电影UIImages.但我不知道该怎么做.
我希望有人可以帮助我或给我一个代码片段,它可以做我喜欢的事情.
编辑:供将来参考 - 应用解决方案后,如果视频看起来扭曲,请确保您捕获的图像/区域的宽度是16的倍数.在这里经过几个小时的努力后发现:
为什么我的UIImages电影得到了扭曲?
这是完整的解决方案(只需确保宽度是16的倍数)
http://codethink.no-ip.org/wordpress/archives/673
在录制视频之前,我在调用摄像机视图时实现了叠加视图.
pickerController.cameraOverlayView =myOverlay;
Run Code Online (Sandbox Code Playgroud)
视频录制并在录制视频和通过电子邮件等共享后将视频保存到相册中一切正常.
如果我将视频质量用作"高品质",那么录制的视频已变得庞大.例如,如果我以高质量录制视频30秒,录制的视频大约为30 - 40 mb.
pickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
Run Code Online (Sandbox Code Playgroud)
如何在共享之前对高质量录制视频进行压缩编程,就像Apple如何使用内置录像机一样?
请指导我解决这个问题.
谢谢!
更新:
这就是我最近的尝试,但仍然没有成功:我想压缩拍摄的录制视频来到didFinishPickingMediaWithInfo并存储在相同的相册实际视频路径本身,而不是其他任何地方.我测试同一个视频被压缩到非常小的尺寸,当我从照片库中选择时,但从相机拍摄的同一视频来自didFinishPickingMediaWithInfo并未压缩,尽管我使用下面的AVAssetExportSession代码.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSString *urlPath = [videoURL path];
if ([[urlPath lastPathComponent] isEqualToString:@"capturedvideo.MOV"])
{
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (urlPath))
{
[self copyTempVideoToMediaLibrary :urlPath];
}
else
{
NSLog(@"Video Capture Error: Captured video cannot be saved...didFinishPickingMediaWithInfo()");
}
}
else
{
NSLog(@"Processing soon to saved photos album...else loop of lastPathComponent..didFinishPickingMediaWithInfo()");
}
}
[self …Run Code Online (Sandbox Code Playgroud) 我正在使用AVAssetWriter,它完全适用于iOS6.
问题是,当我打电话时finishWritingWithCompletionHandler,iOS7 GM上没有调用完成处理程序.
我打电话markAsFinished,甚至endSessionAtSourceTime在我调用finishWritingWithCompletionHandler之前.
它在iOS6上运行良好.
甚至更多,在iOS7上,它可以工作一段时间,然后它再也不起作用了.
我不知道为什么,但如果我使用警报视图调用该方法,它就可以工作.于是,我就performSelectorOnMainThread和inBackground,但它并没有帮助.
有任何想法吗?
我正在尝试用UIImagePickerController压缩用户摄像头拍摄的视频(不是现有的视频,而是一个即时的视频)上传到我的服务器并花费少量时间这样做,所以较小的尺寸是理想的而不是30-新品质相机45毫升.
这是在iOS 8中使用swift进行压缩的代码,它压缩得非常好,我从35 mb轻松到2.1 mb.
func convertVideo(inputUrl: NSURL, outputURL: NSURL)
{
//setup video writer
var videoAsset = AVURLAsset(URL: inputUrl, options: nil) as AVAsset
var videoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0] as AVAssetTrack
var videoSize = videoTrack.naturalSize
var videoWriterCompressionSettings = Dictionary(dictionaryLiteral:(AVVideoAverageBitRateKey,NSNumber(integer:960000)))
var videoWriterSettings = Dictionary(dictionaryLiteral:(AVVideoCodecKey,AVVideoCodecH264),
(AVVideoCompressionPropertiesKey,videoWriterCompressionSettings),
(AVVideoWidthKey,videoSize.width),
(AVVideoHeightKey,videoSize.height))
var videoWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: videoWriterSettings)
videoWriterInput.expectsMediaDataInRealTime = true
videoWriterInput.transform = videoTrack.preferredTransform
var videoWriter = AVAssetWriter(URL: outputURL, fileType: AVFileTypeQuickTimeMovie, error: nil)
videoWriter.addInput(videoWriterInput)
var videoReaderSettings: [String:AnyObject] = [kCVPixelBufferPixelFormatTypeKey:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange]
var videoReaderOutput = AVAssetReaderTrackOutput(track: videoTrack, outputSettings: videoReaderSettings)
var …Run Code Online (Sandbox Code Playgroud) 我们在iOS上捕捉视频的同时使用setPreferredVideoStabilizationMode:AVCaptureVideoStabilizationModeAuto,但视频仍然有时出来模糊在开始和结束时(中间细,虽然),因为我们抢第一帧作为静止图像(以这是很成问题无需切换相机模式即可启用视频和照片功能).
将设备平放在桌面上可以消除所有模糊现象,因此整个视频都非常清晰.这表明它与视频稳定有关,但还有其他属性可以设置吗?
锁定焦点模式是否重要?
还有其他疑难解答吗?
以下是PBJVision的视频捕捉功能,我们使用它:
- (void)startVideoCapture
{
if (![self _canSessionCaptureWithOutput:_currentOutput] || _cameraMode != PBJCameraModeVideo) {
[self _failVideoCaptureWithErrorCode:PBJVisionErrorSessionFailed];
DLog(@"session is not setup properly for capture");
return;
}
DLog(@"starting video capture");
[self _enqueueBlockOnCaptureVideoQueue:^{
if (_flags.recording || _flags.paused)
return;
NSString *guid = [[NSUUID new] UUIDString];
NSString *outputFile = [NSString stringWithFormat:@"video_%@.mp4", guid];
if ([_delegate respondsToSelector:@selector(vision:willStartVideoCaptureToFile:)]) {
outputFile = [_delegate vision:self willStartVideoCaptureToFile:outputFile];
if (!outputFile) {
[self _failVideoCaptureWithErrorCode:PBJVisionErrorBadOutputFile];
return;
}
}
NSString *outputDirectory = (_captureDirectory == nil ? NSTemporaryDirectory() : _captureDirectory); …Run Code Online (Sandbox Code Playgroud) 我知道如何使用AVAssetReader和AVAssetWriter,并成功地使用它们从一部电影中获取视频轨道并将其转码为另一部电影.但是,我也想用音频来做这件事.在完成初始转码后,我是否必须创建和AVAssetExportSession,或者在写入会话期间是否有某种方式在轨道之间切换?我不想处理AVAssetExportSession的开销.
我问,因为,使用拉式方法 - (while([assetWriterInput isReadyForMoreMediaData]){...} - 仅假设一个轨道.它如何用于多个轨道,即音频和视频轨道?
TLDR:跳至更新。我正在寻找一种压缩或降低视频输出质量的方法,最好不要在创建后立即进行,但是如果那是唯一的方法,那就这样吧
另外,如果您知道任何可以完成此任务的优质可可豆荚,那就太好了。
我正在寻找一个可以输出压缩URL的函数,并且我应该能够控制压缩质量...
尝试使函数在当前状态下工作后,它不起作用。Yeilding nil。我认为是由于以下原因:
let outputURL = urlToCompress
assetWriter = try AVAssetWriter(outputURL: outputURL, fileType: AVFileType.mov)
Run Code Online (Sandbox Code Playgroud)
我正在尝试快速压缩视频。到目前为止,所有解决方案都已在创建过程中使用。我想知道创建后是否有压缩方式?仅使用视频网址?
如果不是,那我该如何做一个压缩功能来压缩视频并返回压缩的URL?
我一直在使用的代码:
func compressVideo(videoURL: URL) -> URL {
let data = NSData(contentsOf: videoURL as URL)!
print("File size before compression: \(Double(data.length / 1048576)) mb")
let compressedURL = NSURL.fileURL(withPath: NSTemporaryDirectory() + NSUUID().uuidString + ".mov")
compressVideoHelperMethod(inputURL: videoURL , outputURL: compressedURL) { (exportSession) in
}
return compressedURL
}
func compressVideoHelperMethod(inputURL: URL, outputURL: URL, handler:@escaping (_ exportSession: AVAssetExportSession?)-> Void) {
let urlAsset = AVURLAsset(url: inputURL, …Run Code Online (Sandbox Code Playgroud) 所以,目前我用它来压缩视频:
func compressVideo(inputURL: NSURL, outputURL: NSURL, handler:(session: AVAssetExportSession)-> Void)
{
let urlAsset = AVURLAsset(URL: inputURL, options: nil)
let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetMediumQuality)
exportSession!.outputURL = outputURL
exportSession!.outputFileType = AVFileTypeQuickTimeMovie
exportSession!.shouldOptimizeForNetworkUse = true
exportSession!.exportAsynchronouslyWithCompletionHandler { () -> Void in
handler(session: exportSession!)
}
}
Run Code Online (Sandbox Code Playgroud)
当我在2秒内录制视频时,大小为4.3 MB,当我在6秒内录制视频时,文件大小为9,3 MB.
任何减小尺寸的提示?
基于这个答案/sf/answers/1122473131/我在Swift中创建了以下代码来压缩视频:
var videoWriter:AVAssetWriter!
var videoWriterInput:AVAssetWriterInput!
var processingQueue:dispatch_queue_t = dispatch_queue_create("processingQueue1", nil)
var processingQueue2:dispatch_queue_t = dispatch_queue_create("processingQueue2", nil)
var audioWriterInput:AVAssetWriterInput!
func encode(){
NSFileManager.defaultManager().removeItemAtURL(self.outputFile, error: nil)
let videoCleanApertureSettings = [AVVideoCleanApertureHeightKey: 720,
AVVideoCleanApertureWidthKey: 1280,
AVVideoCleanApertureHorizontalOffsetKey: 2,
AVVideoCleanApertureVerticalOffsetKey: 2
]
let codecSettings = [AVVideoAverageBitRateKey: 1024000,
AVVideoCleanApertureKey: videoCleanApertureSettings
]
let videoSettings = [AVVideoCodecKey: AVVideoCodecKey,
AVVideoCompressionPropertiesKey: codecSettings,
AVVideoHeightKey: 720, AVVideoWidthKey: 1280]
//setup video writer
var error:NSError?
let asset = AVURLAsset(URL: self.inputFile, options: nil)
let videoTrack:AVAssetTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0] as AVAssetTrack
let videoSize:CGSize = videoTrack.naturalSize
videoWriterInput = AVAssetWriterInput(mediaType: …Run Code Online (Sandbox Code Playgroud) 我将AVVideoAverageBitRateKey设置为900000,但我录制的视频获得了不同的比特率值.总是不同的价值,有时850k,有时780k,810k,从不900k.为什么?
这是我做的设置:
NSNumber *compression = [NSNumber numberWithLong:900000];
AVAsset *videoAsset = [[AVURLAsset alloc] initWithURL:inputURL options:nil];
AVAssetTrack *videoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
CGSize videoSize = videoTrack.naturalSize;
NSDictionary *videoWriterCompressionSettings = [NSDictionary dictionaryWithObjectsAndKeys:compression, AVVideoAverageBitRateKey, nil];
NSDictionary *videoWriterSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecH264,
AVVideoCodecKey, videoWriterCompressionSettings,
AVVideoCompressionPropertiesKey, [NSNumber numberWithFloat:videoSize.width],
AVVideoWidthKey, [NSNumber numberWithFloat:videoSize.height],
AVVideoHeightKey, nil];
AVAssetWriterInput* videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoWriterSettings];
videoWriterInput.expectsMediaDataInRealTime = YES;
videoWriterInput.transform = videoTrack.preferredTransform;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:outputURL fileType:AVFileTypeQuickTimeMovie error:nil];
[videoWriter addInput:videoWriterInput];
Run Code Online (Sandbox Code Playgroud) 我正在使用 UIImagePickerController 从我的 swift iOS 应用程序中选择一个视频。我正在保存此 URL,现在想将其转换为数据以使用以下方法发送到我的服务器进行存储:
let messageVideoData = NSData(contentsOfURL: chosenVideoURL)
Run Code Online (Sandbox Code Playgroud)
问题是文件大小非常大。对于在我的 iPhone 6s 上拍摄的 7 秒视频,分辨率为 1280、720,帧速率为 30,文件大小超过 4 MB。我注意到用 whatsapp 和其他聊天应用程序发送的相同图像减少到几百 KB。
减少外部存储文件大小的最佳方法是什么?该视频主要针对手机,因此将分辨率降低到 800 或更低就可以了。
我尝试将 UIImagePickerController 质量设置为:
picker.videoQuality = UIImagePickerControllerQualityType.Type640x480
Run Code Online (Sandbox Code Playgroud)
但这只会将文件大小减少到 3.5 MB。
使用:
picker.videoQuality = UIImagePickerControllerQualityType.TypeLow
Run Code Online (Sandbox Code Playgroud)
将分辨率降低到远低于所需的值。
我应该采取另一种方法来减少存储在服务器上的视频文件大小吗?
我尝试过以下方法:
- (void)compress:(NSURL *)videoPath completionBlock:(void(^)(id data, BOOL result))block{
self.outputFilePath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
NSURL *outputURL = [NSURL fileURLWithPath:self.outputFilePath];
[self compressVideoWithURL:self.movieURL outputURL:outputURL handler:^(AVAssetExportSession *exportSession) {
}];
}
- (void)compressVideoWithURL:(NSURL*)inputURL
outputURL:(NSURL*)outputURL
handler:(void (^)(AVAssetExportSession*))handler {
AVURLAsset *asset = [AVURLAsset assetWithURL:self.movieURL];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
exportSession.fileLengthLimit = 3000000;
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
exportSession.shouldOptimizeForNetworkUse = YES;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
NSData *newOutputData = [NSData dataWithContentsOfURL:outputURL];
NSLog(@"Size of New Video(bytes):%d",[newOutputData length]);
}];
}
Run Code Online (Sandbox Code Playgroud)
我知道self.movieUrl不是 …
ios ×9
swift ×5
avfoundation ×4
video ×4
iphone ×3
compression ×2
avasset ×1
avplayer ×1
corrupt ×1
export ×1
ios7 ×1
pbjvision ×1
transcoding ×1
uiimage ×1
xcode ×1