我必须在视频中应用不透明度。我必须在一秒钟的视频结束前应用它。我正在使用“firstInstruction”来获取视频的总时长。然而,当我调用“firstInstruction.setOpacityRamp”方法时,我无法减去第二个..
let mainInstruction = AVMutableVideoCompositionInstruction()
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeAdd(firstAsset.duration, secondAsset.duration))
let firstInstruction = VideoHelper.videoCompositionInstruction(firstTrack, asset: firstAsset)
firstInstruction.setOpacityRamp(fromStartOpacity: 1, toEndOpacity: 0.1, timeRange: mainInstruction.timeRange)
最佳答案
我会使用三个指令来应用交叉淡入淡出:
所以,首先,让我们了解一下轨道:
import AVFoundation
import CoreVideo
func crossFade(asset0: AVAsset, asset1: AVAsset, crossFadeDuration: CMTime, to outputUrl: URL) throws {
guard
let asset0Track = asset0.tracks(withMediaType: .video).first,
let asset1Track = asset1.tracks(withMediaType: .video).first,
case let composition = AVMutableComposition(),
case let compositionTrack0Id = composition.unusedTrackID(),
let compositionTrack0 = composition.addMutableTrack(
withMediaType: .video, preferredTrackID: compositionTrack0Id),
case let compositionTrack1Id = composition.unusedTrackID(),
let compositionTrack1 = composition.addMutableTrack(
withMediaType: .video, preferredTrackID: compositionTrack1Id)
else { return }
现在让我们计算我们需要的所有时间。首先,组合中 asset0Track 的整个范围,包括直通和交叉淡入淡出期间:
// When does asset0Track start, in the composition?
let asset0TrackStartTime = CMTime.zero
// When does asset0Track end, in the composition?
let asset0TrackEndTime = asset0TrackStartTime + asset0Track.timeRange.duration
接下来,交叉淡入淡出的时间范围:
// When does the cross-fade end, in the composition?
// It should end exactly at the end of asset0's video track.
let crossFadeEndTime = asset0TrackEndTime
// When does the cross-fade start, in the composition?
let crossFadeStartTime = crossFadeEndTime - crossFadeDuration
// What is the entire time range of the cross-fade, in the composition?
let crossFadeTimeRange = CMTimeRangeMake(
start: crossFadeStartTime,
duration: crossFadeDuration)
接下来,合成中 asset1Track 的整个范围,包括交叉淡入淡出和直通期间:
// When does asset1Track start, in the composition?
// It should start exactly at the start of the cross-fade.
let asset1TrackStartTime = crossFadeStartTime
// When does asset1Track end, in the composition?
let asset1TrackEndTime = asset1TrackStartTime + asset1Track.timeRange.duration
最后,两个传递时间范围:
// What is the time range during which only asset0 is visible, in the composition?
let compositionTrack0PassThroughTimeRange = CMTimeRangeMake(
start: asset0TrackStartTime,
duration: crossFadeStartTime - asset0TrackStartTime)
// What is the time range during which only asset1 is visible, in the composition?
let compositionTrack1PassThroughTimeRange = CMTimeRangeMake(
start: crossFadeEndTime,
duration: asset1TrackEndTime - crossFadeEndTime)
现在我们可以将输入轨道插入合成的轨道中:
// Put asset0Track into compositionTrack0.
try compositionTrack0.insertTimeRange(
asset0Track.timeRange,of: asset0Track, at: asset0TrackStartTime)
// Put asset1Track into compositionTrack1.
try compositionTrack1.insertTimeRange(
asset1Track.timeRange, of: asset1Track, at: asset1TrackStartTime)
这就是我们需要为 AVMutableComposition 做的所有事情。但是我们还需要制作一个AVMutableVideoComposition:
let videoComposition = AVMutableVideoComposition()
videoComposition.frameDuration =
min(asset0Track.minFrameDuration, asset1Track.minFrameDuration)
videoComposition.renderSize = CGSize(
width: max(asset0Track.naturalSize.width, asset1Track.naturalSize.width),
height: max(asset0Track.naturalSize.height, asset1Track.naturalSize.height))
我们需要设置视频合成的指令。第一条指令是在适当的时间范围内仅通过 compositionTrack0:
// I'm using a helper function defined below.
let compositionTrack0PassThroughInstruction = AVMutableVideoCompositionInstruction.passThrough(
trackId: compositionTrack0Id, timeRange: compositionTrack0PassThroughTimeRange)
第二个指令是交叉淡入淡出,所以比较复杂。它需要两个子指令,一个用于交叉淡入淡出中的每一层。每一层指令,以及整体交叉淡入淡出指令,使用相同的时间范围:
let crossFadeLayer0Instruction = AVMutableVideoCompositionLayerInstruction()
crossFadeLayer0Instruction.trackID = compositionTrack0Id
crossFadeLayer0Instruction.setOpacityRamp(fromStartOpacity: 1, toEndOpacity: 0, timeRange: crossFadeTimeRange)
let crossFadeLayer1Instruction = AVMutableVideoCompositionLayerInstruction()
crossFadeLayer1Instruction.trackID = compositionTrack1Id
crossFadeLayer1Instruction.setOpacityRamp(fromStartOpacity: 0, toEndOpacity: 1, timeRange: crossFadeTimeRange)
let crossFadeInstruction = AVMutableVideoCompositionInstruction()
crossFadeInstruction.timeRange = crossFadeTimeRange
crossFadeInstruction.layerInstructions = [crossFadeLayer0Instruction, crossFadeLayer1Instruction]
第三条指令是在适当的时间范围内仅通过 compositionTrack1:
let compositionTrack1PassThroughInstruction = AVMutableVideoCompositionInstruction.passThrough(
trackId: compositionTrack1Id, timeRange: compositionTrack1PassThroughTimeRange)
现在我们已经有了所有三个指令,我们可以将它们提供给视频组合:
videoComposition.instructions = [compositionTrack0PassThroughInstruction, crossFadeInstruction, compositionTrack1PassThroughInstruction]
现在我们可以一起使用composition和videoComposition,例如导出一个新的电影文件:
let export = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetMediumQuality)!
export.outputURL = outputUrl
export.videoComposition = videoComposition
export.exportAsynchronously {
exit(0)
}
}
这是我用来创建传递指令的助手:
extension AVMutableVideoCompositionInstruction {
static func passThrough(trackId: CMPersistentTrackID, timeRange: CMTimeRange) -> AVMutableVideoCompositionInstruction {
let layerInstruction = AVMutableVideoCompositionLayerInstruction()
layerInstruction.trackID = trackId
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = timeRange
instruction.layerInstructions = [layerInstruction]
return instruction
}
}
这是我的测试代码。我使用 macOS 命令行应用程序进行测试:
let asset0 = AVURLAsset(url: URL(fileURLWithPath: "/tmp/asset0.mp4"))
let asset1 = AVURLAsset(url: URL(fileURLWithPath: "/tmp/asset1.mp4"))
let outputUrl = URL(fileURLWithPath: "/tmp/output.mp4")
try! crossFade(asset0: asset0, asset1: asset1, crossFadeDuration: CMTimeMake(value: 1, timescale: 1), to: outputUrl)
dispatchMain()
结果:
请注意,由于 Stack Overflow 对图像文件大小的限制,我不得不将动画制作得很小且颜色很浅。
输入视频由 Jeffrey Beach 提供.
关于ios - 计算视频之间淡入淡出的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54119791/
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
这里是Ruby新手。完成一些练习后碰壁了。练习:计算一系列成绩的字母等级创建一个方法get_grade来接受测试分数数组。数组中的每个分数应介于0和100之间,其中100是最大分数。计算平均分并将字母等级作为字符串返回,即“A”、“B”、“C”、“D”、“E”或“F”。我一直返回错误:avg.rb:1:syntaxerror,unexpectedtLBRACK,expecting')'defget_grade([100,90,80])^avg.rb:1:syntaxerror,unexpected')',expecting$end这是我目前所拥有的。我想坚持使用下面的方法或.join,
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
这里有一个很好的答案解释了如何在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返回它复制的字节数,但是当我还没有下
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build
我正在检查一个Rails项目。在ERubyHTML模板页面上,我看到了这样几行:我不明白为什么不这样写:在这种情况下,||=和ifnil?有什么区别? 最佳答案 在这种特殊情况下没有区别,但可能是出于习惯。每当我看到nil?被使用时,它几乎总是使用不当。在Ruby中,很少有东西在逻辑上是假的,只有文字false和nil是。这意味着像if(!x.nil?)这样的代码几乎总是更好地表示为if(x)除非期望x可能是文字false。我会将其切换为||=false,因为它具有相同的结果,但这在很大程度上取决于偏好。唯一的缺点是赋值会在每次运行
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里