4

I'm using following library to convert mkv to mp4: https://github.com/SterlingOnLoop/FFmpegWrapper.

- (void)convertUsingFFmpegWrapper {
    NSString *mp4Extension = @"mp4";
    NSString *mkvExtension = @"mkv";

    NSString *videoName = @"file1";
//    NSString *videoName = @"file2";

    NSString *mkvVideoFilePath = [[NSBundle mainBundle] pathForResource:videoName ofType:mkvExtension];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = paths[0];
    NSString *mp4VideoFilePath = [NSString stringWithFormat:@"%@/%@.%@", documentsDirectory, videoName, mp4Extension];

    FFmpegWrapper *ffmpegWrapper = [[FFmpegWrapper alloc] init];
    NSDictionary *options = @{kFFmpegInputFormatKey: mkvExtension, kFFmpegOutputFormatKey: mp4Extension};
    [ffmpegWrapper convertInputPath:mkvVideoFilePath outputPath:mp4VideoFilePath options:options progressBlock:nil completionBlock:^(BOOL success, NSError *error) {
        if (success && !error) {
            // delete mp4 file
        } else if (error) {
            NSLog(@"Error during .MKV -> .MP4 conversion occured: %@", error.localizedDescription);
        } else {
            NSLog(@"Unknown error during .MKV -> .MP4 conversion occured.");
        }
    }];
}

Here are the values from LLDB about the automatically detected codec types:

(lldb) po inputStream.codecName
aac

(lldb) po outputStream.codecName
aac

I should mention here, that originally the file is generated on Linux with following codecs: vaapiencode_h264 for video and faac for sound.

The issue is that the file simply does not work. I get huge amount of logs in the console, where most important is:

[aac @ 0x7f7f65019200] Format aac detected only with low score of 1, misdetection possible!

Inside the library, the following function is used:

int streamInfoValue = avformat_find_stream_info(inputFormatContext, NULL);

And exactly this line does the whole mess with the logs. Obviously, without this line I receive an error with invalid argument.

When that line is turned on, the .mp4 file is generated. It lasts > 5 minutes, while input file is 11 seconds long. It cannot be played using VLC on my mac (seems to be broken). I get following errors (I'm pasting few of them, full track can be found here, it's too long to quote it here):

[aac @ 0x7ff07c00b000] More than one AAC RDB per ADTS frame is not implemented. Update your FFmpeg version to the newest one from Git. If the problem still occurs, it means that your file has a feature which has not been implemented.
[aac @ 0x7ff07c00b000] channel element 0.0 is not allocated
[aac @ 0x7ff07c00b000] Sample rate index in program config element does not match the sample rate index configured by the container.
[aac @ 0x7ff07c00b000] Inconsistent channel configuration.
[aac @ 0x7ff07c00b000] get_buffer() failed
[aac @ 0x7ff07c00b000] channel element 3.10 is not allocated
[aac @ 0x7ff07c00b000] Reserved bit set.
[aac @ 0x7ff07c00b000] invalid band type
[aac @ 0x7ff07c00b000] Number of scalefactor bands in group (50) exceeds limit (41).
[aac @ 0x7ff07c00b000] Reserved bit set.
[aac @ 0x7ff07c00b000] Number of bands (7) exceeds limit (4).
[aac @ 0x7ff07c00b000] Reserved bit set.
[aac @ 0x7ff07c00b000] Dependent coupling is not supported together with LTP

Any idea how to simply convert mkv to mp4? I don't have any idea why the errors occurs. I'd claim that the file is not aac, but the linux uses this encoding, so it should be valid.

Nat
  • 12,032
  • 9
  • 56
  • 103
  • All your errors come from one thing, the so-called AAC audio. Is it possible to put a temp link to the MKV file for analysing? – VC.One Jul 14 '16 at 22:09
  • @VC.One Sure, thank you! :) Here is the link for small (4MB) mkv file: http://s000.tinyupload.com/?file_id=05753933961175453657 – Nat Jul 15 '16 at 12:39
  • @Vive I just wanted to know how did you integrated this library as I also need to integrate this. I could not able to integrate this library. Can you please help me? Thanks in advance – Akshaykumar Maldhure May 17 '17 at 06:48
  • @AkshaykumarMaldhure I've used private cocoapods, but for easier way you have the installation steps on the top of the project's readme. – Nat May 17 '17 at 06:49
  • @Vive Yes i followed the same but no success, can you please help me to install it, like I followed the first step git submodule add Submodules/FFmpegWrapper https://github.com/OpenWatch/FFmpegWrapper.git but it does not add include and lib folder. Its very important for me to get this integrated. – Akshaykumar Maldhure May 17 '17 at 06:54
  • @AkshaykumarMaldhure What do you mean by "it does not add include and lib folder"..? Where and what does not add..? You need to perform `git submodule init` and `git submodule update` after you add this submodule to fetch the data. You might need to set non-arc flag to some source files, I already don't remember. – Nat May 17 '17 at 09:47
  • @Vive Thanks, I will try with your suggestion – Akshaykumar Maldhure May 17 '17 at 10:31
  • @AkshaykumarMaldhure This discussion is not in a proper place. Please create a question, link it here (add a comment with a link) and please remove your other comments here as they're irrelevant for this problem. I'll visit your question after you link it. – Nat May 17 '17 at 13:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144476/discussion-between-akshaykumar-maldhure-and-vive). – Akshaykumar Maldhure May 17 '17 at 15:02

1 Answers1

1

I've checked your file. The main issues I can see is that :

  • There is no audio bitrate (ie: 128kbps should be set)
  • Somehow the metadata lists the AAC version as 4 (what does that even mean??).
  • The codec ID should be 40 but in the MKV's metadata it's listed as A_AAC/MPEG4/LC
  • There is no flag for Channel Position in the bytes (ie: Front L+R)

Anyways ffmpeg (command line) converted your MKV without any errors.
Let me know if this new re-encoded MP4 version works okay on your side.

The ffmpeg settings were simply : ffmpeg -i mkv_demo.mkv -c:v copy demo_new.mp4

Unfortunately I don't code in iOS so I couldn't show you a "fixed" code. Maybe someone can help later on. Look at this other Answer for extra hints (make it work like shown command line snippet above)...

Community
  • 1
  • 1
VC.One
  • 14,790
  • 4
  • 25
  • 57
  • Hi VC.One, firstly thanks for your in-depth help! How did you check all of the following info, which program did you use? It's better to know how to fish instead of getting a fish :). Once again, I really appreciate the help, as I know quite little about audio formats and that gave me a strong lesson. Re-encoded MP4 works perfectly. I will try to update the metadata and then the conversion. – Nat Jul 18 '16 at 07:09
  • Also I've tried to check out that codecID information, and `A_AAC/MPEG5/LC` seems to be valid according to: https://matroska.org/technical/specs/codecid/index.html. Why it isn't valid? – Nat Jul 18 '16 at 07:20
  • 1
    While your answer is valid, I've just ended with using C implementation and it worked. So it seems that the wrapper I've tried to use, was invalid. – Nat Aug 10 '16 at 14:07
  • 1
    Glad it's fixed for you. The fish analogy is wise.. For future media analysis you can try **MediaInfo** (freeware tool). There is also **FFprobe** (part of FFmpeg) as a possible option. I also just happen to be familiar with MP4 and AAC bytes, so I could directly check the file bytes in some hex editor. – VC.One Aug 10 '16 at 18:22