omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Merging downloaded Audio and Video

    Pythonista
    video merge audio
    4
    5
    2506
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • EGC
      EGC last edited by 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
              }
          }
          ];
      
      }
      
      mikael 1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • mikael
          mikael @EGC last edited by

          @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.

          1 Reply Last reply Reply Quote 0
          • JonB
            JonB last edited by

            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

            1 Reply Last reply Reply Quote 0
            • Miyatehe
              Miyatehe last edited by

              This post is deleted!
              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Powered by NodeBB Forums | Contributors