Comments (3)
You may try Timeline.audios
. Set your audio to it and specific the audio's timerange
from cabbage.
Hi @vitoziv so for the last week I tried using Timeline.audios. After lot's of experimenting I figured out how the API works. But I had some issues. For example, Im trying to set a music asset as the background audio for the entire video composition, and IF the video composition is longer than the music asset, then I repeat the audio from the beginning by calculating how many times the Duration of the music asset can fit the Video.
video: [------------------------------]
audio: [----][----][----][----][----][--]
In the example above the audio asset can fit 5 times in the video and a 6th time with a reduced timeRange. That's where I got problems with the calculations.
And the end I had to substract 0.05 to the last timeRange END value, otherwise it would fail.
let tLine = Timeline()
var vChannel = [TrackItem]()
var aChannel = [TrackItem]()
for clip: G8Clip in self._clips {
if let asset = clip.asset {
let resource = AVAssetTrackResource(asset: asset)
// Create a TrackItem instance, TrackItem can configure video&audio configuration
let trackItem = TrackItem(resource: resource)
// Set the video scale mode on canvas
trackItem.configuration.videoConfiguration.baseContentMode = .aspectFit
trackItem.configuration.audioConfiguration.volume = 0.0
vChannel.append(trackItem)
aChannel.append(trackItem)
}
}
let musicUrl = Bundle.main.url(forResource: "HumansWater_Short5", withExtension: "m4a")!
let musicAsset = AVAsset(url: musicUrl)
self.musicCabbageResource = AVAssetTrackResource(asset: musicAsset)
self.musicCabbageResource!.prepare(completion: { (status, error) in
//Note: When Audio is longer than the whole video composition is when it can fail. That's why we Loop the audio
if status == VFCabbage.Resource.ResourceStatus.avaliable {
print(">> Total VIDEO DURATION: \(self.totalDuration.seconds)")
print(">> Music File Duration: \(musicAsset.duration.seconds)")
let numOfLoops = self.totalDuration.seconds/musicAsset.duration.seconds
let numOfLoopsRoundedUp = numOfLoops.rounded(.up)
var sumPartsTotals = CMTime.zero
var endS = CMTime.zero
for i in 0..<Int(numOfLoopsRoundedUp) {
let mResource = AVAssetTrackResource(asset: musicAsset)
//Audio Trim
let start = CMTimeMake(value: Int64(0.0 * 600), timescale: 600)
if(i == Int(numOfLoopsRoundedUp)-1){ //is the last chunk of audio
let lastChunkTimeFrac = numOfLoops.truncatingRemainder(dividingBy: 1) // ex 1.5 will give 0.5
let lastChunkTimeSecs = musicAsset.duration.seconds * lastChunkTimeFrac //music from 0 to this value
endS = CMTimeMake(value: Int64((lastChunkTimeSecs-0.05) * 600), timescale: 600)
}else{
endS = CMTimeMake(value: Int64(musicAsset.duration.seconds * 600), timescale: 600)
}
mResource.selectedTimeRange = CMTimeRange(start:start , end: endS)
let part_mTrackItem = TrackItem(resource: mResource)
part_mTrackItem.startTime = CMTimeMultiply(musicAsset.duration, multiplier: Int32(i))
print("start:\(part_mTrackItem.startTime.seconds) - totalPart:\(mResource.scaledDuration.seconds)")
sumPartsTotals = CMTimeAdd(sumPartsTotals, mResource.scaledDuration)
tLine.audios.append(part_mTrackItem)
}
print(">> Total Duration of Audio Parts:\(sumPartsTotals.seconds) - should be same as Total Video Duration")
tLine.videoChannel = vChannel
tLine.audioChannel = aChannel
try! Timeline.reloadVideoStartTime(providers: tLine.videoChannel)
try! Timeline.reloadAudioStartTime(providers: tLine.audioChannel)
If I remove the - 0.05 the calculations are correct. But the composition FAILS, because I think that for some reason the tLine.audios total duration end up larger than the video.
For now it works if I do - 0.05. but do you have a better suggestion ?
Thanks anyways.
As always great Library. !! 👏👏👍
from cabbage.
Nothing wrong with your code. Maybe it's because of the double
calculation, try do less double
calculation.
from cabbage.
Related Issues (20)
- player background blur effect HOT 2
- 添加片段过多内存暴增
- Player add blur background HOT 6
- Use Kingfisher to report an error
- About the text overlay, edit, drag and drop, zoom. HOT 1
- CIImage Memory leak HOT 2
- Overlay H.265 Video with Transparency HOT 4
- option to change number of frames on export
- Sound gone when scaled video HOT 1
- Multiple timelines HOT 1
- black screen when the audio overlay goes to finish HOT 2
- transition demo not work HOT 1
- 如何知道轨道和片段之间的关系
- Is this framework support multiple layer blend mode? HOT 1
- Capture the current frame in composed player item HOT 1
- avplayer seek后,影音不同步 HOT 1
- Exported video appears to be darker (colour shifting) HOT 3
- Orientation problem: some videos are rotated by 90 or 180 degrees
- Example of merging several videos in sequence
- Example project doesn't work on Xcode 15 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cabbage.