我有一个功能,应该在iphone/ipad上将视频重新编码为可管理的比特率.这是:*更新的工作代码,现在有音频!:)*
-(void)resizeVideo:(NSString*)pathy{
NSString *newName = [pathy stringByAppendingString:@".down.mov"];
NSURL *fullPath = [NSURL fileURLWithPath:newName];
NSURL *path = [NSURL fileURLWithPath:pathy];
NSLog(@"Write Started");
NSError *error = nil;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:fullPath fileType:AVFileTypeQuickTimeMovie error:&error];
NSParameterAssert(videoWriter);
AVAsset *avAsset = [[[AVURLAsset alloc] initWithURL:path options:nil] autorelease];
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:1280], AVVideoWidthKey,
[NSNumber numberWithInt:720], AVVideoHeightKey,
nil];
AVAssetWriterInput* videoWriterInput = [[AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings] retain];
NSParameterAssert(videoWriterInput);
NSParameterAssert([videoWriter canAddInput:videoWriterInput]);
videoWriterInput.expectsMediaDataInRealTime = YES;
[videoWriter addInput:videoWriterInput];
NSError *aerror = nil;
AVAssetReader *reader = [[AVAssetReader alloc] …
Run Code Online (Sandbox Code Playgroud) 我正在使用a AVAssetReaderOutput
来读取a中的样本AVAsset
,对它们进行一些处理,并使用RemoteIO AU播放结果.
问题是,在调用AudioOutputUnitStop
暂停播放后,在转到后台并返回前台后,调用后音频将不会再次启动AudioOutputUnitStart
.这是由于copyNextSampleBuffer
方法返回的错误,该错误AVAssetReaderOutput
被称为渲染管道的一部分.
调用之后的status
属性是,其属性为Error Domain = AVFoundationErrorDomain Code = -11847"Operation Interrupted"UserInfo = 0x1d8b6100 {NSLocalizedRecoverySuggestion =停止其他操作并再试一次.,NSLocalizedDescription = Operation Interrupted}AVAssetReader
copyNextSampleBuffer
AVAssetReaderStatusFailed
error
我正在寻找一种解决方案,它不会强迫我在回到前台后重新初始化整个管道 - 希望有这样一个解决方案,AVAssetReader
s可以在应用程序进入后台并返回...
笔记
AVAudioSession
在AVAudioSessionDelegate
s endInterruptionWithFlags:
事件和应用程序变为活动状态时将我设置为活动状态.无论我是否这样做都没有区别,得到同样的错误.一些代码:
AudioPlayer
@implementation AudioPlayer
...
// Audio Unit Setup
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_RemoteIO;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
AudioComponent defaultOutput …
Run Code Online (Sandbox Code Playgroud) 我希望从视频文件中的音轨中提取样本精确的LPCM音频范围.目前,我正在寻找实现这一目标用AVAssetReaderTrackOutput
对一个AVAssetTrack
从读取交付AVURLAsset
.
尽管准备并确保使用AVURLAssetPreferPreciseDurationAndTimingKey
set to 初始化资产,但在资产中YES
寻找样本准确的位置似乎是不准确的.
NSDictionary *options = @{ AVURLAssetPreferPreciseDurationAndTimingKey : @(YES) };
_asset = [[AVURLAsset alloc] initWithURL:fileURL options:options];
Run Code Online (Sandbox Code Playgroud)
这表现为例如可变比特率编码的AAC流.虽然我知道VBR音频流在准确搜索时会产生性能开销,但我愿意为此付费,前提是我提供了准确的样本.
当使用例如扩展音频文件服务和ExtAudioFileRef
API时,我可以实现样本精确的搜索和音频提取.同样地AVAudioFile
,因为它建立在之上ExtAudioFileRef
.
然而,问题是我还想从媒体容器中提取音频文件只有API拒绝但在AVFoundation 中支持的音频AVURLAsset
.
提取的样本准确时间范围使用CMTime
和定义CMTimeRange
,并设置在AVAssetReaderTrackOutput
.然后迭代地提取样本.
-(NSData *)readFromFrame:(SInt64)startFrame
requestedFrameCount:(UInt32)frameCount
{
NSUInteger expectedByteCount = frameCount * _bytesPerFrame;
NSMutableData *data = [NSMutableData dataWithCapacity:expectedByteCount];
//
// Configure Output
//
NSDictionary *settings = @{ AVFormatIDKey : @( kAudioFormatLinearPCM ),
AVLinearPCMIsNonInterleaved …
Run Code Online (Sandbox Code Playgroud) 你如何通过AVAssetReader读取样本?我找到了使用AVAssetReader进行复制或混合的示例,但这些循环始终由AVAssetWriter循环控制.是否可以只创建一个AVAssetReader并通读它,获取每个样本?
谢谢.
我正在尝试使用本地m4a或mp3文件并压缩/下采样此文件(用于制作较小的文件).
最初,我使用AVAssetExportSession将AVAsset导出到临时目录,但我没有对压缩/下采样的任何控制(你只能使用预设,其中只有.wav文件格式支持质量下降) .
然后,按照SO上的几个例子,我尝试使用AVAssetReader/AVAssetWriter来预先形成这个'export'.
我这样创建我的读者/作者:
NSString *exportPath = [NSHomeDirectory() stringByAppendingPathComponent:@"out.m4a"];
NSURL *exportURL = [NSURL fileURLWithPath:outPath];
// reader
NSError *readerError = nil;
AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:asset
error:&readerError];
AVAssetTrack *track = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
AVAssetReaderTrackOutput *readerOutput = [[AVAssetReaderTrackOutput alloc] initWithTrack:track
outputSettings:nil];
[reader addOutput:readerOutput];
// writer
NSError *writerError = nil;
AVAssetWriter *writer = [[AVAssetWriter alloc] initWithURL:exportURL
fileType:AVFileTypeAppleM4A
error:&writerError];
AudioChannelLayout channelLayout;
memset(&channelLayout, 0, sizeof(AudioChannelLayout));
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
// use different values to affect the downsampling/compression
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: …
Run Code Online (Sandbox Code Playgroud) 在这个问题中实现了编码视频(带音频)的解决方案,使用AVAssetWriter的视频编码 - CRASHES后,我发现代码在iPhone模拟器中正常工作.不幸的是,某些视频在实际的iPhone 5(和其他设备)上运行时无法对其音频进行编码.
例如,从WWDC 2011示例代码RosyWriter(https://developer.apple.com/library/IOS/samplecode/RosyWriter/Introduction/Intro.html)生成的视频不会完全编码,因为该函数-[AVAssetReaderOutput copyNextSampleBuffer]
永远不会返回.
视频缓冲区正确输入,但一旦尝试复制第一个音频CMSampleBufferRef,呼叫就会挂起.当我在其他来源的视频上尝试此操作时,例如原生iOS相机应用中记录的视频,音频导入正确.
此线程https://groups.google.com/forum/#!topic/coreaudio-api/F4cqCu99nUI记录了copyNextSampleBuffer
与AudioQueues一起使用时挂起的功能,并建议将操作保留在单个线程上.我已经尝试将所有内容保存在主线程上的单独线程中,但没有运气.
有没有其他人经历过这个并有可能的解决方案?
编辑:从RosyWriter生成的视频相对于来自本机Camera应用程序的视频,即音频流作为流0,视频流作为流1,其视频相反.
Stream #0:0(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 60 kb/s
Metadata:
creation_time : 2013-10-28 16:13:05
handler_name : Core Media Data Handler
Stream #0:1(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 1920x1080, 8716 kb/s, 28.99 fps, 29.97 tbr, 600 tbn, 1200 tbc
Metadata:
rotate : 90
creation_time : 2013-10-28 16:13:05
handler_name : Core Media Data Handler …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在iOS上播放视频(MP4/H.263),但结果非常模糊.这是初始化资产阅读的代码:
mTextureHandle = [self createTexture:CGSizeMake(400,400)];
NSURL * url = [NSURL fileURLWithPath:file];
mAsset = [[AVURLAsset alloc] initWithURL:url options:NULL];
NSArray * tracks = [mAsset tracksWithMediaType:AVMediaTypeVideo];
mTrack = [tracks objectAtIndex:0];
NSLog(@"Tracks: %i", [tracks count]);
NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary * settings = [[NSDictionary alloc] initWithObjectsAndKeys:value, key, nil];
mOutput = [[AVAssetReaderTrackOutput alloc]
initWithTrack:mTrack outputSettings:settings];
mReader = [[AVAssetReader alloc] initWithAsset:mAsset error:nil];
[mReader addOutput:mOutput];
Run Code Online (Sandbox Code Playgroud)
对于读者init来说,现在是实际的纹理:
CMSampleBufferRef sampleBuffer = [mOutput copyNextSampleBuffer];
CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
glBindTexture(GL_TEXTURE_2D, mTextureHandle);
glTexImage2D(GL_TEXTURE_2D, …
Run Code Online (Sandbox Code Playgroud) 我试图反过来制作视频.在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
我试图转换Bob McCune的Learning AVFoundation书中的一个例子,并使用AVAssetReader和NSInputStream来解决一些问题.图形应该是纯正弦波,但值似乎以某种方式反映在X轴上.
我已经尝试过每次迭代的字节交换,我都能想到并且没有用.
Playground在这里发布到github:https: //github.com/justinlevi/AVAssetReader
//: Playground - noun: a place where people can play
import UIKit
import AVFoundation
import XCPlayground
func plotArrayInPlayground<T>(arrayToPlot:Array<T>, title:String) {
for currentValue in arrayToPlot {
XCPCaptureValue(title, value: currentValue)
}
}
class SSSampleDataFilter {
var sampleData:NSData?
init(data:NSData) {
sampleData = data
}
func filteredSamplesForSize(size:CGSize) -> [Int]{
var filterSamples = [UInt16]()
if let sampleData = sampleData {
let sampleCount = sampleData.length
let binSize = CGFloat(sampleCount) / size.width
let stream = NSInputStream(data: sampleData)
stream.open()
var …
Run Code Online (Sandbox Code Playgroud) 我们正在使用AVAssetReader
和使用AVAssetWriter的视频编码中AVAssetWriter
提到的样式- CRASHES基本上是从视频库/资产库中读取视频,然后以不同的比特率写入以减小其大小(用于最终的网络上传) .
让它为我们工作的诀窍是kCVPixelBufferPixelFormatTypeKey
在outputSettings
on中指定一个键和值AVAssetReaderTrackOutput
,如下所示:
NSDictionary *outputSettings = [NSDictionary
dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
forKey:(id)kCVPixelBufferPixelFormatTypeKey];
readerTrackOutput =
[[AVAssetReaderTrackOutput alloc] initWithTrack:src_track
outputSettings:outputSettings];
Run Code Online (Sandbox Code Playgroud)
所以基本上我们使用kCVPixelFormatType_32BGRA
了kCVPixelBufferPixelFormatTypeKey
键的值.
但显然我们可以选择许多可能的像素格式类型.在iOS 6.0.1 iPhone设备上运行技术问答QA1501:核心视频 - 可用像素格式中记录的代码,以下是它显示的支持像素格式类型列表:
Core Video Supported Pixel Format Types:
Core Video Pixel Format Type: 32
Core Video Pixel Format Type: 24
Core Video Pixel Format Type: 16
Core Video Pixel Format Type (FourCC): L565
Core Video Pixel Format Type (FourCC): 2vuy …
Run Code Online (Sandbox Code Playgroud) avassetreader ×10
avfoundation ×6
ios ×6
objective-c ×3
avasset ×2
core-audio ×2
iphone ×2
audio ×1
audiounit ×1
cocoa-touch ×1
core-video ×1
ipad ×1
opengl-es ×1
swift2 ×1
video ×1