Forum Archive

Merging downloaded Audio and Video

EGC

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
     }
 }
 ];

}
```

JonB

To call a c function, if you use

from objc_util import *

Then the variable c is the cell that you can access. Generally you need to define argtypes and restype of c functions. Use c_void_p for Objc objects.

mikael

@EGC, in addition to the function signature, other challenging bits are probably constant values, CMTime structs and the block. All are doable, but someone needs to have a bit of time in their hands to do the whole conversion.

JonB

there have been a few people, maybe myself included, that have implemented all of the CMTime structs and such. search google for objc_util and cmtime.. for instance

https://github.com/tdamdouni/Pythonista/blob/master/_2017/avplayer.py

Miyatehe

To merge downloaded audio and video files, I usually make use of Joyoshare Video Joiner. It can merge multiple media files at 60X speed without quality loss.