我有一个应用程序,允许用户录制视频,UIImagePickerController
然后将其上传到YouTube.问题是UIImagePickerController
创建的视频文件是巨大的,即使视频只有5秒长.例如,5秒长的视频是16-20兆字节.我想保持540或720质量的视频,但我想减少文件大小.
我一直在尝试AVFoundation并AVAssetExportSession
试图获得更小的文件大小.我试过以下代码:
AVAsset *video = [AVAsset assetWithURL:videoURL];
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:video presetName:AVAssetExportPresetPassthrough];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.outputURL = [pathToSavedVideosDirectory URLByAppendingPathComponent:@"vid1.mp4"];
[exportSession exportAsynchronouslyWithCompletionHandler:^{
NSLog(@"done processing video!");
}];
Run Code Online (Sandbox Code Playgroud)
但这并没有减少文件大小.我知道我正在做的事情是可能的,因为在Apple的照片应用程序中,当您选择"在YouTube上分享"时,会自动处理视频文件,因此它的小到可以上传.我想在我的应用程序中做同样的事情.
我怎么能做到这一点?
我正在尝试使用图片阵列和音频文件制作电影文件.为了制作带有图片阵列的电影,我在这里使用了zoul的大帖子.一切都很完美,我的电影和我的照片一样.但是,当我尝试添加一些音轨时,我遇到了很多问题.要理解我把我的代码:
当我调用此方法时,图片数组和歌曲文件已准备就绪:
-(void) writeImagesToMovieAtPath:(NSString *) path withSize:(CGSize) size
{
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *dirContents = [[NSFileManager defaultManager] directoryContentsAtPath:documentsDirectoryPath];
for (NSString *tString in dirContents) {
if ([tString isEqualToString:@"essai.mp4"])
{
[[NSFileManager defaultManager]removeItemAtPath:[NSString stringWithFormat:@"%@/%@",documentsDirectoryPath,tString] error:nil];
}
}
NSLog(@"Write Started");
NSError *error = nil;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
[NSURL fileURLWithPath:path] fileType:AVFileTypeMPEG4
error:&error];
NSParameterAssert(videoWriter);
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:size.width], AVVideoWidthKey,
[NSNumber numberWithInt:size.height], AVVideoHeightKey,
nil];
AudioChannelLayout channelLayout;
memset(&channelLayout, 0, sizeof(AudioChannelLayout));
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
NSDictionary …
Run Code Online (Sandbox Code Playgroud) 我已经看过几次这个问题了,但是他们似乎没有任何合适的答案.
要求是反转并输出视频文件(不仅仅是反向播放),保持与源视频相同的压缩,格式和帧速率.
理想情况下,解决方案能够在内存或缓冲区中完成所有操作,并避免将帧生成到图像文件中(例如:使用AVAssetImageGenerator
),然后重新编译它(资源密集,不可靠的计时结果,帧/图像质量从原始,等等.).
-
我的贡献:这仍然没有用,但到目前为止我尝试过的最好:
CMSampleBufferRef[]
使用的数组AVAssetReader
.AVAssetWriter
.CMSampleBufferRef
所以即使向后追加它们也行不通.AVAssetWriter
.下一步:我要调查一下 AVAssetWriterInputPixelBufferAdaptor
- (AVAsset *)assetByReversingAsset:(AVAsset *)asset {
NSURL *tmpFileURL = [NSURL URLWithString:@"/tmp/test.mp4"];
NSError *error;
// initialize the AVAssetReader that will read the input asset track
AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:asset error:&error];
AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] lastObject];
AVAssetReaderTrackOutput* readerOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:videoTrack outputSettings:nil];
[reader addOutput:readerOutput];
[reader startReading];
// Read in the samples into an array
NSMutableArray *samples …
Run Code Online (Sandbox Code Playgroud)我试图从图像magick库给出的图像创建一个视频文件.像不透明度差异一样逐个应用一些效果后,它会成功创建,但会Quick time player
给出错误" video file could not be opened. The movie's file format isn't recognized "
.
我使用以下代码:
double d = 0.00;
- (void)posterizeImageWithCompression:(id)sender {
// Here we use JPEG compression.
NSLog(@"we're using JPEG compression");
MagickWandGenesis();
magick_wand = NewMagickWand();
magick_wand = [self magiWandWithImage:[UIImage imageNamed:@"iphone.png"]];
MagickBooleanType status;
status = MagickSetImageOpacity(magick_wand, d);
if (status == MagickFalse) {
ThrowWandException(magick_wand);
}
if (status == MagickFalse) {
ThrowWandException(magick_wand);
}
size_t my_size;
unsigned char * my_image = MagickGetImageBlob(magick_wand, &my_size); …
Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我需要捕获视频并在该视频上添加水印.水印应为文本(时间和注释).我看到使用"QTKit"Frame工作的代码.但是我读到该框架不适用于iPhone.
提前致谢.
这段代码大部分都有效,但结果数据似乎松散了一个颜色通道(正如我所想),因为显示的结果图像数据是蓝色的!
这是代码:
UIImage* myImage=[UIImage imageNamed:@"sample1.png"];
CGImageRef imageRef=[myImage CGImage];
CVImageBufferRef pixelBuffer = [self pixelBufferFromCGImage:imageRef];
Run Code Online (Sandbox Code Playgroud)
方法pixelBufferFromCGIImage是从stackoverflow上的另一个帖子中获取的:如何将UIImage数组导出为电影?(虽然这个应用程序与我想要做的事情无关)但确实如此
+ (CVPixelBufferRef)pixelBufferFromCGImage:(CGImageRef)image
{
CGSize frameSize = CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image));
NSDictionary *options = @{
(__bridge NSString *)kCVPixelBufferCGImageCompatibilityKey: @(NO),
(__bridge NSString *)kCVPixelBufferCGBitmapContextCompatibilityKey: @(NO)
};
CVPixelBufferRef pixelBuffer;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, frameSize.width,
frameSize.height, kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef) options,
&pixelBuffer);
if (status != kCVReturnSuccess) {
return NULL;
}
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
void *data = CVPixelBufferGetBaseAddress(pixelBuffer);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(data, frameSize.width, frameSize.height,
8, CVPixelBufferGetBytesPerRow(pixelBuffer), rgbColorSpace,
(CGBitmapInfo) kCGImageAlphaNoneSkipLast);
CGContextDrawImage(context, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将单个视频与单个图像组合在一起.这不是试图将许多图像组合成单个视频,例如
我AVMutableComposition
用来组合曲目.我的应用程序能够组合视频和图像(但就目前而言,视频组合很好!)我尝试使用AVAssetWriter
将单个图像转换为视频(我相信这是我的问题,但不是100%肯定) .然后我将其保存到app(documents directory
).从那里,我在合并中访问它,并将视频和现在变成视频的图像组合在一起.
流程:
用户选择图像 - >
将图像转换为AVAssetWriter以更改为视频 - >
合并我已预设的视频视频 - >
结果:从所选图像和预设视频制作1个视频.
我的问题:我的代码提供了一个空白区域,视频内部的图像应该是.就像在我的ImageConverter文件中一样,它会将它转换为视频,但我只会看到最后一帧作为图像,而每隔一帧都是透明的,好像图片不存在一样.因此,如果我将图像转换为视频5秒钟(假设是30帧/秒),那么我将看到(30*5)-1帧的空白区域,然后是最后一帧,最终会出现图片.我正在寻找有关如何将单个图像制作成视频或将视频和图像组合在一起而不将图像转换为视频的指导.谢谢!
合并文件在这里
func merge() {
if let firstAsset = controller.firstAsset, secondAsset = self.asset {
// 1 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances.
let mixComposition = AVMutableComposition()
let firstTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeVideo,
preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
do {
try firstTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, CMTime(seconds: 8, preferredTimescale: 600)),
ofTrack: firstAsset.tracksWithMediaType(AVMediaTypeVideo)[0] ,
atTime: kCMTimeZero)
} …
Run Code Online (Sandbox Code Playgroud) 我想将a转换CGImage
为CMSampleBufferRef
并AVAssetWriterInput
使用该appendSampleBuffer:
方法将其附加到a .我已经设法CMSampleBufferRef
使用以下代码,但appendSampleBuffer:
只是NO
在我提供结果时返回CMSampleBufferRef
.我究竟做错了什么?
- (void) appendCGImage: (CGImageRef) frame
{
const int width = CGImageGetWidth(frame);
const int height = CGImageGetHeight(frame);
// Create a dummy pixel buffer to try the encoding
// on something simple.
CVPixelBufferRef pixelBuffer = NULL;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, width, height,
kCVPixelFormatType_32BGRA, NULL, &pixelBuffer);
NSParameterAssert(status == kCVReturnSuccess && pixelBuffer != NULL);
// Sample timing info.
CMTime frameTime = CMTimeMake(1, 30);
CMTime currentTime = …
Run Code Online (Sandbox Code Playgroud) 我正在使用zoul的解决方案将UIImage数组导出为电影.但我的镜架都变形了.这是原始图片:
这是一个类似的失真例子:http: //oi56.tinypic.com/8wkqys.jpg
我在这里读到它与纵横比有关,但没有解释如何修复它.
我已经完成了研发工作并取得了成功,如何根据播放的视频文件中的图像获取帧MPMoviePlayerController
.
从此代码中获取所有帧,并将所有图像保存在一个阵列中.
for(int i= 1; i <= moviePlayerController.duration; i++)
{
UIImage *img = [moviePlayerController thumbnailImageAtTime:i timeOption:MPMovieTimeOptionNearestKeyFrame];
[arrImages addObject:img];
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是,在更改了一些图像文件后,比如为图像添加情感并添加过滤器,例如; 电影真实,黑白,我们如何再次创建视频并以相同的帧速率将相同的视频存储在文档目录中,而不会丢失视频质量.
更改了一些图像后,我按照代码再次保存了该视频.
- (void) writeImagesAsMovie:(NSString*)path
{
NSError *error = nil;
UIImage *first = [arrImages objectAtIndex:0];
CGSize frameSize = first.size;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
[NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
error:&error];
NSParameterAssert(videoWriter);
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:640], AVVideoWidthKey,
[NSNumber numberWithInt:480], AVVideoHeightKey,
nil];
AVAssetWriterInput* writerInput = [[AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings] retain];
AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
sourcePixelBufferAttributes:nil];
NSParameterAssert(writerInput); …
Run Code Online (Sandbox Code Playgroud) ios ×6
avfoundation ×5
iphone ×5
objective-c ×5
uiimage ×3
video ×3
audio ×1
bitmap ×1
buffer ×1
core-media ×1
core-video ×1
export ×1
file-upload ×1
imagemagick ×1
macos ×1
swift ×1
watermark ×1