Ios: Remove Audio from Video

Swift - Remove audio from video

with the help of this link I wrote this code & this worked for me...

var mutableVideoURL = NSURL() //final video url
func removeAudioFromVideo(_ videoURL: URL) {
let inputVideoURL: URL = videoURL
let sourceAsset = AVURLAsset(url: inputVideoURL)
let sourceVideoTrack: AVAssetTrack? = sourceAsset.tracks(withMediaType: AVMediaTypeVideo)[0]
let composition : AVMutableComposition = AVMutableComposition()
let compositionVideoTrack: AVMutableCompositionTrack? = composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
let x: CMTimeRange = CMTimeRangeMake(kCMTimeZero, sourceAsset.duration)
_ = try? compositionVideoTrack!.insertTimeRange(x, of: sourceVideoTrack!, at: kCMTimeZero)
mutableVideoURL = NSURL(fileURLWithPath: NSHomeDirectory() + "/Documents/FinalVideo.mp4")
let exporter: AVAssetExportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)!
exporter.outputFileType = AVFileTypeMPEG4
exporter.outputURL = mutableVideoURL as URL
removeFileAtURLIfExists(url: mutableVideoURL)
exporter.exportAsynchronously(completionHandler:
{
switch exporter.status
{
case AVAssetExportSessionStatus.failed:
print("failed \(exporter.error)")
case AVAssetExportSessionStatus.cancelled:
print("cancelled \(exporter.error)")
case AVAssetExportSessionStatus.unknown:
print("unknown\(exporter.error)")
case AVAssetExportSessionStatus.waiting:
print("waiting\(exporter.error)")
case AVAssetExportSessionStatus.exporting:
print("exporting\(exporter.error)")
default:
print("-----Mutable video exportation complete.")
}
})
}

func removeFileAtURLIfExists(url: NSURL) {
if let filePath = url.path {
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath) {
do{
try fileManager.removeItem(atPath: filePath)
} catch let error as NSError {
print("Couldn't remove existing destination file: \(error)")
}
}
}
}

Ios: Remove audio from video

// Remove audio from video     
- (void) RemoveAudioFromVideo:(NSString *)VideoLocalPath {
NSString * initPath1 = VideoLocalPath;
AVMutableComposition *composition = [AVMutableComposition composition];

NSString *inputVideoPath = initPath1;
AVURLAsset * sourceAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:inputVideoPath] options:nil];

AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

BOOL ok = NO;

AVAssetTrack * sourceVideoTrack = [[sourceAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

CMTimeRange x = CMTimeRangeMake(kCMTimeZero, [sourceAsset duration]);

ok = [compositionVideoTrack insertTimeRange:x ofTrack:sourceVideoTrack atTime:kCMTimeZero error:nil];

if([[NSFileManager defaultManager] fileExistsAtPath:initPath1]) {
[[NSFileManager defaultManager] removeItemAtPath:initPath1 error:nil];
}

NSURL *url = [[NSURL alloc] initFileURLWithPath: initPath1];

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];

exporter.outputURL=url;

NSLog(@";%@", [exporter supportedFileTypes]);

exporter.outputFileType = @"com.apple.quicktime-movie";
[exporter exportAsynchronouslyWithCompletionHandler:^{
[self savefinalVideoFileToDocuments:exporter.outputURL];
}];
}

// Write final Video
-(void)savefinalVideoFileToDocuments:(NSURL *)url {
NSString *storePath = [[self applicationCacheDirectory] stringByAppendingPathComponent:@"Videos"];
NSData * movieData = [NSData dataWithContentsOfURL:url];
[movieData writeToFile:storePath atomically:YES];
}

// Directory Path
- (NSString *)applicationCacheDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return documentsDirectory;
}

FFMPEG Removing audio from video makes the video short

The video stream is variable frame rate and the last frame has a timestamp of 5m34s. The audio is longer. Players will hold on the last frame till the longest stream (audio in this case) has been fully played.

The extraction is fine.

How to turn off audio from a video using AVAudioPlayer?

So I see that actually what needs to be done here is all the video data which has audio needs to be removed as seen bellow

        self.playerQueue?.removeAllItems()

But if you want to get rid of audio you just need to do .pause() and then remove the data. here is docs

swift: how to delete part of audio?

CMTimeRangeGetUnion returns another CMTimeRange, which is just a (start-)time and a duration. So there is nothing than can hold the two time ranges required to do what you are expecting. In extension, AVAssetExportSession has no API that takes a list of time ranges to export.

But there is a way to accomplish it. The idea is to create an editable copy of the asset, delete the time range, and then export the editable copy. AVMutableComposition does this:

// assuming 'asset', 'endTimeOfRange1' and 'startTimeOfRange2' from the question:

// create empty mutable composition
let composition: AVMutableComposition = AVMutableComposition()
// copy all of original asset into the mutable composition, effectively creating an editable copy
try composition.insertTimeRange( CMTimeRangeMake( kCMTimeZero, asset.duration), of: asset, at: kCMTimeZero)

// now edit as required, e.g. delete a time range
let startTime = CMTime(seconds: endTimeOfRange1, preferredTimescale: 100)
let endTime = CMTime(seconds: startTimeOfRange2, preferredTimescale: 100)
composition.removeTimeRange( CMTimeRangeFromTimeToTime( startTime, endTime))

// since AVMutableComposition is an AVAsset subclass, it can be exported with AVAssetExportSession (or played with an AVPlayer(Item))
if let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
{
// configure session and exportAsynchronously as above.
// You don't have to set the timeRange of the exportSession
}

Note that copying from the asset to the composition only modifies some in-memory structures defining which samples go where on the time line, but doesn't actually moves any media samples around. This is not done until exporting; as result, editing is (relatively) fast, and you have to keep the source file around at least until export is finished.

MPMoviePlayerViewController remove Audio and Subtitles control

Here is the reason why the button is there, and the way to hide it :

Video Player unexpectedly shows Alternate Track button for Subtitles and Captions

The source material will have to include: CLOSED-CAPTIONS=NONE on the EXT-X-STREAM-INF tag to remove the Button.

Edit :

I've been searching on "how to insert HTTP headers for a URL" and found a few interesting results. First, if you really want to insert HTTP headers to your URL, the following might do the job :

NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setValue:@"NONE" forHTTPHeaderField:@"CLOSED-CAPTIONS"];

If it's not what you're searching for, the solution suggested here could make the job :

I think setting a cookie might solve your problem. Please look into the documentation for NSHTTPCookie and NSHTTPCookieStorage.

If it's still not working, take a look at this related question.

Finally, if all those steps didn't help, implement your own control bar thanks to this tutorial.

WebRTC iOS, how to mute video call

What do you mean by newest source? ARDAppClient should has the adding of local stream's Audio and Video Component.

Here is the working solution:

static NSString * const kARDAudioTrackId = @"ARDAMSa0";

//Initialization part
_factory = [[RTCPeerConnectionFactory alloc] init];
RTCAudioTrack *_localAudioTrack = [_factory audioTrackWithSource:source
trackId:kARDAudioTrackId];

//For mute and unmute the local stream Audio
- (void)toggleAudioMute {
self.audioMute = !self.audioMute;
_localAudioTrack.isEnabled = !self.audioMute;
}


Related Topics



Leave a reply



Submit