I'm looking a way to merge the video and audio files downloaded with youtube-dl (All using Pythonista, obviously.)
Has anyone done this?
I found some objective-c code in StackOverflow for example:
https://stackoverflow.com/questions/14618362/how-to-merge-audio-in-our-video-file
https://stackoverflow.com/questions/13909400/merging-audio-with-video-objective-c
I picked the second but haven't gone too far.
ns_video_path = nsurl(video_path)
ns_audio_path = nsurl(audio_path)
ns_out_video_path = nsurl(out_video_path)
AVMutableComposition = ObjCClass('AVMutableComposition')
mixComposition = AVMutableComposition.composition()
nextClipStartTime = CMTime.in_dll(c,'kCMTimeZero')
videoAsset= ObjCClass('AVURLAsset').alloc().initWithURL_options_(ns_video_path,None)
# How to call a objective-c function? is there a bridge for them?
# I reviewed objc_util http://omz-software.com/pythonista/docs/ios/objc_util.html
#
# CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
#
Here's one sample from StackOverflow
-(void)mergeAudioVideo
{
NSString *videoOutputPath=[_documentsDirectory stringByAppendingPathComponent:@"dummy_video.mp4"];
NSString *outputFilePath = [_documentsDirectory stringByAppendingPathComponent:@"final_video.mp4"];
if ([[NSFileManager defaultManager]fileExistsAtPath:outputFilePath])
[[NSFileManager defaultManager]removeItemAtPath:outputFilePath error:nil];
NSURL *outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
NSString *filePath = [_documentsDirectory stringByAppendingPathComponent:@"newFile.m4a"];
AVMutableComposition* mixComposition = [AVMutableComposition composition];
NSURL *audio_inputFileUrl = [NSURL fileURLWithPath:filePath];
NSURL *video_inputFileUrl = [NSURL fileURLWithPath:videoOutputPath];
CMTime nextClipStartTime = kCMTimeZero;
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:video_inputFileUrl options:nil];
CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil];
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = outputFileUrl;
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void ) {
if (_assetExport.status == AVAssetExportSessionStatusCompleted) {
//Write Code Here to Continue
}
else {
//Write Fail Code here
}
}
];
}