我一天中的大部分时间都在围绕 StackOverflow 进行深入研究,虽然有很多关于该主题的精彩帖子,但我还没有找到解决我的问题的方法。 p>
我正在使用 AVAssetWriter 编写视频文件,没有问题。我的视频文件,如果我保存到我的相机胶卷,可以正确播放并按预期方向播放。以下是我的设置方式;
init(fileUrl:URL!, height:Int, width:Int) {
// Setup the filter writer instance
fileWriter = try? AVAssetWriter(outputURL: fileUrl, fileType: AVFileType.mov)
// Setup the video settings
let videoOutputSettings: Dictionary<String, AnyObject> = [
AVVideoCodecKey : AVVideoCodecType.hevc as AnyObject,
AVVideoWidthKey : width as AnyObject,
AVVideoHeightKey : height as AnyObject
]
// Setup the attributes dictionary
let sourcePixelBufferAttributesDictionary = [
String(kCVPixelBufferPixelFormatTypeKey) : Int(kCVPixelFormatType_32BGRA),
String(kCVPixelBufferWidthKey) : Int(width),
String(kCVPixelBufferHeightKey) : Int(height),
String(kCVPixelFormatOpenGLESCompatibility) : kCFBooleanTrue
] as [String : Any]
// Setup the video input
videoInput = AVAssetWriterInput(mediaType: AVMediaType.video, outputSettings: videoOutputSettings)
// Data should be expected in real time
videoInput.expectsMediaDataInRealTime = true
// Perform transform
videoInput.transform = CGAffineTransform(rotationAngle: CGFloat(CGFloat.pi / 2.0))
// Setup pixel buffer intput
assetWriterPixelBufferInput = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: videoInput,
sourcePixelBufferAttributes: sourcePixelBufferAttributesDictionary)
// Add the input
fileWriter.add(videoInput)
}
然后我想使用 AVMutableComposition 来保存应用了图像叠加层的视频,它可以正常工作,除了视频方向不正确;
func postProcessVideo(toFPS: Double, sourceVideo: URL, destination: URL, filterImage: UIImage?, completionHandler: @escaping (_ response: Bool) -> ()) {
// Log
print("Received call to begin post-processing video at:", sourceVideo)
// Instantiate the AVMutableComposion
let composition = AVMutableComposition()
// Setup the video asset
let vidAsset = AVURLAsset(url: sourceVideo, options: [:])
// Get video track
let vtrack = vidAsset.tracks(withMediaType: AVMediaType.video)
// Setup the first video track as asset track
let videoTrack: AVAssetTrack = vtrack[0]
// Setup the video timerange
let vid_timerange = CMTimeRangeMake(kCMTimeZero, vidAsset.duration)
// Setup the composition video track
let compositionvideoTrack:AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: CMPersistentTrackID())!
// Insert expected time range
do {
try compositionvideoTrack.insertTimeRange(vid_timerange, of: videoTrack, at: kCMTimeZero)
} catch {
}
// Setup the preferred transform
compositionvideoTrack.preferredTransform = videoTrack.preferredTransform
// Update time scale
let finalTimeScale: Int64 = vidAsset.duration.value * 3
// Adjust video track duration
compositionvideoTrack.scaleTimeRange(CMTimeRangeMake(kCMTimeZero, vidAsset.duration), toDuration: CMTimeMake(finalTimeScale, vidAsset.duration.timescale))
// Setup effect size
let size = videoTrack.naturalSize
// Setup the image
let imglogo = UIImage(named: "gif1.png")
let imglayer = CALayer()
imglayer.contents = imglogo?.cgImage
imglayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
imglayer.opacity = 0.0
// Setup the video layer
let videolayer = CALayer()
// Setup the video layer frame
videolayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
// Setup the parent layer
let parentlayer = CALayer()
// Setup the parent layer frame
parentlayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
// Add video layer
parentlayer.addSublayer(videolayer)
// Add filter layer
parentlayer.addSublayer(imglayer)
// Setup the layer composition
let layercomposition = AVMutableVideoComposition()
// Setup the desired frame rate
layercomposition.frameDuration = CMTimeMake(1, Int32(toFPS))
// Setup the render size
layercomposition.renderSize = size
// Setup the animation tool
layercomposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videolayer, in: parentlayer)
// Setup instruction for filter overlay
let instruction = AVMutableVideoCompositionInstruction()
// Setup the desired time range
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration)
// Setup video track
let videotrack = composition.tracks(withMediaType: AVMediaType.video)[0]
// Setup layer instruction
let layerinstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videotrack)
// Setup layer instructions
instruction.layerInstructions = [layerinstruction]
// Setup layer composition instructions
layercomposition.instructions = [instruction]
// Instantiate the asset export
let assetExport = AVAssetExportSession(asset: composition, presetName:AVAssetExportPresetHighestQuality)
// Setup the video composition
assetExport?.videoComposition = layercomposition
// Setup the output file type
assetExport?.outputFileType = AVFileType.mov
// Setup the destination
assetExport?.outputURL = destination
// Export video
assetExport?.exportAsynchronously(completionHandler: {
switch assetExport?.status{
case .failed?:
print("failed \(assetExport!.error)")
case .cancelled?:
print("cancelled \(assetExport!.error)")
default:
print("Movie complete")
completionHandler(true)
}
})
}
很抱歉太长了,但是是否有任何突出的因素可以帮助解释导出过程中的方向变化?
谢谢!
最佳答案
我遇到了关于方向的问题,我是这样解决的:
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrack setPreferredTransform:CGAffineTransformRotate(CGAffineTransformMakeScale(-1, 1), M_PI)];
通过旋转和缩放它。它是在 objective-C 中,但你可以很容易地转换它。你只需要改变这个:
// Setup the preferred transform
compositionvideoTrack.preferredTransform = videoTrack.preferredTransform
而不是 preferredTransform 手动给出转换。
关于ios - AVMutableComposition 没有正确定位视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47745814/
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
question的一些答案关于redirect_to让我想到了其他一些问题。基本上,我正在使用Rails2.1编写博客应用程序。我一直在尝试自己完成大部分工作(因为我对Rails有所了解),但在需要时会引用Internet上的教程和引用资料。我设法让一个简单的博客正常运行,然后我尝试添加评论。靠我自己,我设法让它进入了可以从script/console添加评论的阶段,但我无法让表单正常工作。我遵循的其中一个教程建议在帖子Controller中创建一个“评论”操作,以添加评论。我的问题是:这是“标准”方式吗?我的另一个问题的答案之一似乎暗示应该有一个CommentsController参
我喜欢使用Textile或Markdown为我的项目编写自述文件,但是当我生成RDoc时,自述文件被解释为RDoc并且看起来非常糟糕。有没有办法让RDoc通过RedCloth或BlueCloth而不是它自己的格式化程序运行文件?它可以配置为自动检测文件后缀的格式吗?(例如README.textile通过RedCloth运行,但README.mdown通过BlueCloth运行) 最佳答案 使用YARD直接代替RDoc将允许您包含Textile或Markdown文件,只要它们的文件后缀是合理的。我经常使用类似于以下Rake任务的东西:
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co
我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent