last day (15 days later) » 

08:10
5
A: How can I reduce the file size of a video created with UIImagePickerController?

Lance SamariaCode for Swift 5 and Good Quality Here's how to do it following code from this link. The problem with the link is it only works with .mov file output, if you want to output a .mp4 file it will crash. The code below lets you get a .mp4 output. It is tried, tested, and works. Example a 15 sec video...

np, we all need to help each other. Cheers!
@DavidKyslenko thanks for pointing out the camel casing issue, I made the update. Cheers!!!
Can you help me I want to compress my audio?
@YogeshPatel I never dealt with only audio before but I would think you would use the same exact code from above but exclude anything that has to do with video such as videoFinished, videoTrack, videoReaderSettings, assetReaderVideoOutput, videoSettings, videoInput, and videoInputQueue. If you remove those variables from the file then it should still work. Message me if it doesn't.
Hey, could you please look once? I added a question regarding this. stackoverflow.com/q/66972501/8201581 Thanks!
@YogeshPatel You didn't try any of the code from my answer. You have to try the code and exclude the parts that I mentioned above and see what happens. Everything is explained on what to do in the answer from the code itself to how to use it. The question you added is closed
08:10
The question was closed because I hadn't added a good question and once I edit that and share the link with you. I tried this code it not reducing my audio storing the same size. I'll try one more time and let you know. Thanks!
@YogeshPatel you must be doing something wrong. I just did exactly what I told you to do earlier, exclude anything that says video. I just tried an audio file from here ee.columbia.edu/~dpwe/sounds/music, the africa-toto.wav file. It was 12.0 mb before compression and then 3.0 mb after compression. I used the same exact code from the answer and just excluded everything that I told you to exclude.
As per your comment I am doing same thing but facing one issue. Constant 'assetReaderAudioOutput' captured by a closure before being initialized Could you help me with this because AVAssetReaderTrackOutput has no init method?
I don't know what you're doing wrong. You need to do in in these steps. (1) remove all of these variables var videoFinished, var videoTrack, var videoReaderSettings, var assetReaderVideoOutput, let videoSettings, var videoInput, and var videoInputQueue. (2) you will get a bunch of red errors, remove all of the red errors. (3) that's it
You need to put break point on this line assetReaderAudioOutput = AVAssetReaderTrackOutput(track: audioTrack, outputSettings: audioReaderSettings). Your error is saying that the assetReaderAudioOutput is nil. If it has no value then it is not getting initialized. The line I told you to put a break point on is where it gets initialized
You must've removed the wrong code. Only remove the code that pertains to video
or just ask a question, it'll be easier. I can just copy and paste the code.
Hey, Thank you so much. Finally it's working charm!
ok great, please up vote if it's working
08:25
Yeah, My original audio size is 6.7MB and now it's 3.0 MB. Really you help me a lot to achieve this. Thanks again. Could you please tell me the last thing. I am getting multiple audio URL and doing this with for loop at that time how can manage this compress thing. Right now for single audio, it's works fine. could you guide me need to this on thread or background thread. It's completion so it complete for loop then it comes to the closure. hope you understand this. Thanks!
I don't understand
The error was we initialise this like
var assetReaderAudioOutput: AVAssetReaderTrackOutput?
And I did this thing then it works
var assetReaderAudioOutput: AVAssetReaderTrackOutput!
Okay we are doing this code for one audio so my question is if we have multiple audios so we are taking a for loop then how can we manage this thing. Sometimes it out of scope the for loop and then compress closure will be called.
I am doing this force fully
var assetReaderAudioOutput: AVAssetReaderTrackOutput!
Is it fine?
(1) I don't know how your loop works but if it were me I'd use a DispatchGroup stackoverflow.com/a/64635915/4833705. You need to make sure you call dispatch.leaveI wouldn't forcefully unwrap it. You must've deleted the wrong code because there is a line
(1) I don't know how your loop works but if it were me I'd use a DispatchGroup stackoverflow.com/a/64635915/4833705. You need to make sure you call dispatch.leave() everywhere it is necessary inside the compression function. I wouldn't forcefully unwrap it. Just **guard let assetReaderAudioOutput = assetReaderAudioOutput else { return } ** before it enters the closure
guard let assetReaderAudioOutput = assetReaderAudioOutput else { return }*; let closeWriter:()->Void = { ... }
08:43
Okay. Thank you. I'll try and let you know once it done.
For your loop you should call that compression function on a background task
 
2 hours later…
10:52
Ok. could you please help me with this? This crashing due to

'*** -[AVAssetReaderTrackOutput copyNextSampleBuffer] cannot copy next sample buffer before adding this output to an instance of AVAssetReader (using -addOutput:) and calling -startReading on that asset reader'

This is line of code:
if let cmSampleBuffer = assetReaderAudioOutput?.copyNextSampleBuffer() {
 
5 hours later…
15:53
That's an obscure question. I don't where to begin because I don't know what you're doing. Is this happening with just 1 audio file compressed by itself or in your for-loop? If it's happening with just 1 solo audio file then you removed the wrong parts of the code. You have to get it working with just 1 audio file first. Only exclude the parts the have the word video in it. After you get that working then you need to do add a parameter for a your DispatchGroup inside the compression function
for example: **func compressFile(_ urlToCompress: URL, dispatchGroup: DispatchGroup, completion:@escaping (URL)->Void) { }
inside that function you have to know when to call dispatchGroup.leave() anywhere where there is a return, or when the function completes. For example your for-loop should be like let group = DispatchGroup(); for audioURL in urls { group.enter(); compressFile(audioURL, dispatchGroup: group) {(compressedURL)...} } and inside the compressFile function itself you have to determine when to call dispatchGroup.leave(). Call group.notify(queue: .main) { ... } outside of the loop
The function itself works fine for me. I never used it in a for-loop but stand alone, when I removed anything that has the word video and I compressed an audio file, I had no problems. If this is where you're having the problem then the issue is you are removing the wrong pieces of code. Only remove the parts that have the word video in it.
16:20
You may want to try a Semaphore instead of a dispatchGroup. Here are 2 really simple answers inside this question. stackoverflow.com/questions/47735204/dispatchgroup-in-for-loop
Actually don't use a semaphore. Read the comments under the question from @Rob. He is an expert on this stuff. DispatchGroup should work fine if you do it correctly
16:41
Try both, use a dispatchGroup and then try a semaphore to see what happens. You can also try an OperationQueue but I have never used one before so I can't give you any advice on it. As I'm sure you already know, when programming you have to try different things. Good Luck!!! Let me know how it works out for you :)

last day (15 days later) »