1

I'm trying to take a file from my iTunes library and produce a clipped/trimmed version of it. The problem I'm running into is that I tell it to trim on a specific time range. It trims with the right duration, but a different start time.

I'm using the export session method given in Trim audio with iOS.

This github project reproduces the issue. It's setup to trim starting at 10sec for a duration of 3 sec. What is produced is a clip with time range at around 0:12.1 to 0:15.1.

https://github.com/nickbolton/AVAssetExportSessionDebug.git

Any ideas as to why the trimmed time range is wrong? Thanks!

Community
  • 1
  • 1
Larvell Jones
  • 159
  • 2
  • 15

1 Answers1

3

There's nothing wrong with your code per se.

The problem you are experiencing is a direct artifact of the fact that you are starting with an MP3. It does not automatically provide accurate duration and timings. You have to ask for them.

You are saying:

AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:self.sourceAudioURL 
    options:nil]; 

It's that nil options that's doing the damage. Instead, simply say:

AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:self.sourceAudioURL 
    options:@{AVURLAssetPreferPreciseDurationAndTimingKey:@YES}];
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Yeah that's your bingo. The problem is the conversion from mp3 to aac for the test file takes about 1.15 sec. Seems very inefficient to have to process the entire file when I'm only interested in a small portion. I guess I can pre-process the file when selected. Thanks, tho! – Larvell Jones Dec 23 '14 at 21:51
  • Sorry, my previous answer was utterly bogus. See my revised answer. This turned out to be much more interesting - and much simpler - than I thought. – matt Dec 23 '14 at 23:16
  • Yeah that is much better. But now I have a similar issue with AVPlayer when asking it for the currentTime. It's not returning a precise time in all cases. Any ideas how to remedy that? I may still need to convert the entire source audio to aac. Because that seems to work.. :-/ – Larvell Jones Dec 24 '14 at 12:59