anthwlock / untrunc

Restore a truncated mp4/mov. Improved version of ponchio/untrunc
GNU General Public License v2.0
2.1k stars 201 forks source link

src/mp4.h:298: bool Mp4::currentChunkFinished(int): Assertion `cur_track_idx == last_track_idx_' failed #174

Open twirtgen opened 3 months ago

twirtgen commented 3 months ago

Hello,

Due to lack of space on the first SD card, the camera continued recording on the second SD card. The content on the first SD card was not saved correctly during the switch.

I tried recover the content, but it seems to detect a length of 0s for all audio and video streams. Using the -s option throws an assertion (src/mp4.h:298: bool Mp4::currentChunkFinished(int): Assertion 'cur_track_idx == last_track_idx_' failed). Details of the logs can be found at the end. Do you have any idea what might be causing this?

I have also tried changing the version of ffmpeg using 3.4.13 and 3.3.9, but the problem is still the same.

Thanks for your help!

Verbose log using `-s` option ```txt Info: version 'v364-bd783dd' using ffmpeg 'n7.0.1' Lavc61.3.100 Info: reading 20240626_FX3_0080.MP4 Info: parsing healthy moov atom ... Composition time offset atom found. Out of order samples possible. [mov,mp4,m4a,3gp,3g2,mj2 @ 0x63ea48205fc0] st: 0 edit list: 1 Missing key frame while searching for timestamp: 1000 [mov,mp4,m4a,3gp,3g2,mj2 @ 0x63ea48205fc0] st: 0 edit list 1 Cannot find an index entry before timestamp: 1000. Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '20240626_FX3_0080.MP4': Metadata: major_brand : XAVC minor_version : 16785407 compatible_brands: XAVCmp42iso2 creation_time : 2024-06-29T19:07:53.000000Z Duration: 00:07:25.44, start: 0.000000, bitrate: 147049 kb/s Stream #0:0[0x1](und): Video: h264 (High 4:2:2) (avc1 / 0x31637661), yuv422p10le(tv, bt709, progressive), 3840x2160 [SAR 1:1 DAR 16:9], 140016 kb/s, 25 fps, 25 tbr, 25k tbn (default) Metadata: creation_time : 2024-06-29T19:07:53.000000Z handler_name : Video Media Handler vendor_id : [0][0][0][0] encoder : AVC Coding Stream #0:1[0x2](und): Audio: pcm_s24be (ipcm / 0x6D637069), 48000 Hz, 1 channels, s32 (24 bit), 1152 kb/s (default) Metadata: creation_time : 2024-06-29T19:07:53.000000Z handler_name : Sound Media Handler vendor_id : [0][0][0][0] Stream #0:2[0x3](und): Audio: pcm_s24be (ipcm / 0x6D637069), 48000 Hz, 1 channels, s32 (24 bit), 1152 kb/s (default) Metadata: creation_time : 2024-06-29T19:07:53.000000Z handler_name : Sound Media Handler vendor_id : [0][0][0][0] Stream #0:3[0x4](und): Audio: pcm_s24be (ipcm / 0x6D637069), 48000 Hz, 1 channels, s32 (24 bit), 1152 kb/s (default) Metadata: creation_time : 2024-06-29T19:07:53.000000Z handler_name : Sound Media Handler vendor_id : [0][0][0][0] Stream #0:4[0x5](und): Audio: pcm_s24be (ipcm / 0x6D637069), 48000 Hz, 1 channels, s32 (24 bit), 1152 kb/s (default) Metadata: creation_time : 2024-06-29T19:07:53.000000Z handler_name : Sound Media Handler vendor_id : [0][0][0][0] Stream #0:5[0x6](und): Data: none (rtmd / 0x646D7472), 2252 kb/s (default) Metadata: creation_time : 2024-06-29T19:07:53.000000Z handler_name : Timed Metadata Media Handler timecode : 00:00:00:00 ftyp_ = 'XAVC' detected 'XAVC', deactivating 'g_strict_nal_frame_check' found avcC after: 102 remaining len:382 parsing avcC ... len_sps: 52 decoding SPS ... log2_max_frame_num: 6 avcC got decoded assuming constant duration of 1000 for 'avc1' (x11136) fallback to save upper_bound upper_bound=23653057 fallback to save upper_bound upper_bound=16832247 assuming constant duration of 1 for 'ipcm' (x21381120) assuming constant duration of 1 for 'ipcm' (x21381120) assuming constant duration of 1 for 'ipcm' (x21381120) assuming constant duration of 1 for 'ipcm' (x21381120) assuming constant duration of 1000 for 'rtmd' (x11136) Info: special track found (meta, 'Timed Metadata Media Handler') Info: unknown track 'ipcm' found -> fallback to dynamic stats created dummy track 'free' removed dummy track 'free' first_failed: 0 of 101 simpleOrder: 5 0 1 2 3 4 order ( 101): (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) first_failed: 0 of 101 order: (5, 12) (0, 12) (1, 23040) (2, 23040) (3, 23040) (4, 23040) genPatternPerm of: avc1 0 avc1 -1 0 1 ipcm -1 0 2 ipcm -1 0 3 ipcm -1 0 4 ipcm -1 0 5 rtmd -1 0 genPatternPerm of: ipcm 0 avc1 -1 0 1 ipcm -1 0 2 ipcm -1 0 3 ipcm -1 0 4 ipcm -1 0 5 rtmd -1 0 genPatternPerm of: ipcm 0 avc1 -1 0 1 ipcm -1 0 2 ipcm -1 0 3 ipcm 0 16 4 ipcm -1 0 5 rtmd -1 0 genPatternPerm of: ipcm 0 avc1 -1 0 1 ipcm -1 0 2 ipcm -1 0 3 ipcm -1 0 4 ipcm 0 32 5 rtmd -1 0 genPatternPerm of: ipcm 0 avc1 -1 0 1 ipcm -1 0 2 ipcm -1 0 3 ipcm -1 0 4 ipcm -1 0 5 rtmd 1.95894 30 genPatternPerm of: rtmd 0 avc1 1.43883 30 1 ipcm -1 0 2 ipcm -1 0 3 ipcm -1 0 4 ipcm -1 0 5 rtmd -1 0 has_zero_transitions_: 1 Info: using dynamic stats, use '-is' to see them Stats: first_off_: 262144 first_off_rel_: 0 max_part_size_: 23653057 avc1 ss_normal: 274452 643882 3561477 | 0 23653057 | 0.55303 1.03747 8.19352 10672 ss_keyframe: 1436331 1992745 3556260 | 995756 16832247 | 0.21509 1.29813 3.64772 464 chunk_distance_gcd_: 1 start_off_gcd_: 1 end_off_gcd_: 1 pkt_sz_gcd_: 1 pad_after_chunk_: 0 likely n_samples/chunk (p=1.00000): 12 likely sample_sizes (p=0.00000): n_mutual_patterns: 0 avc1_ipcm (0->1) [0] (928) avc1_ipcm (0->2) [0] (0) avc1_ipcm (0->3) [0] (0) avc1_ipcm (0->4) [0] (0) avc1_rtmd (0->5) [0] (0) ipcm ss_normal: 3 3 3 | 3 3 | 0.00000 0.00000 0.00000 100 ss_keyframe: 3 3 3 | 3 3 | 0.00000 0.00000 0.00000 100 chunk_distance_gcd_: 1 start_off_gcd_: 1 end_off_gcd_: 1 pkt_sz_gcd_: 1 pad_after_chunk_: 0 likely n_samples/chunk (p=1.00000): 23040 likely sample_sizes (p=1.00000): 3 n_mutual_patterns: 0 ipcm_avc1 (1->0) [0] (0) ipcm_ipcm (1->2) [0] (928) ipcm_ipcm (1->3) [0] (0) ipcm_ipcm (1->4) [0] (0) ipcm_rtmd (1->5) [0] (0) ipcm ss_normal: 3 3 3 | 3 3 | 0.00000 0.00000 0.00000 100 ss_keyframe: 3 3 3 | 3 3 | 0.00000 0.00000 0.00000 100 chunk_distance_gcd_: 1 start_off_gcd_: 1 end_off_gcd_: 1 pkt_sz_gcd_: 1 pad_after_chunk_: 0 likely n_samples/chunk (p=1.00000): 23040 likely sample_sizes (p=1.00000): 3 n_mutual_patterns: 1 ipcm_ipcm (2->3) [1] (928) 1.000 100 ________ ________ ________ ________ 00000000 00000000 00000000 00000000 ipcm_avc1 (2->0) [0] (0) ipcm_ipcm (2->1) [0] (0) ipcm_ipcm (2->4) [0] (0) ipcm_rtmd (2->5) [0] (0) ipcm ss_normal: 3 3 3 | 3 3 | 0.00000 0.00000 0.00000 100 ss_keyframe: 3 3 3 | 3 3 | 0.00000 0.00000 0.00000 100 chunk_distance_gcd_: 1 start_off_gcd_: 1 end_off_gcd_: 1 pkt_sz_gcd_: 1 pad_after_chunk_: 0 likely n_samples/chunk (p=1.00000): 23040 likely sample_sizes (p=1.00000): 3 n_mutual_patterns: 1 ipcm_ipcm (3->4) [1] (928) 1.000 100 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ipcm_avc1 (3->0) [0] (0) ipcm_ipcm (3->1) [0] (0) ipcm_ipcm (3->2) [0] (0) ipcm_rtmd (3->5) [0] (0) ipcm ss_normal: 3 3 3 | 3 3 | 0.00000 0.00000 0.00000 100 ss_keyframe: 3 3 3 | 3 3 | 0.00000 0.00000 0.00000 100 chunk_distance_gcd_: 1 start_off_gcd_: 1 end_off_gcd_: 1 pkt_sz_gcd_: 1 pad_after_chunk_: 0 likely n_samples/chunk (p=1.00000): 23040 likely sample_sizes (p=1.00000): 3 n_mutual_patterns: 1 ipcm_rtmd (4->5) [1] (927) 1.000 100 00000000 00000000 00000000 00000000 001c0100 09712273 f0010010 2000____ ipcm_avc1 (4->0) [0] (0) ipcm_ipcm (4->1) [0] (0) ipcm_ipcm (4->2) [0] (0) ipcm_ipcm (4->3) [0] (0) rtmd ss_normal: 11264 11264 11264 | 11264 11264 | 0.00000 0.00000 0.00000 100 ss_keyframe: 11264 11264 11264 | 11264 11264 | 0.00000 0.00000 0.00000 100 chunk_distance_gcd_: 1 start_off_gcd_: 1 end_off_gcd_: 1 pkt_sz_gcd_: 1 pad_after_chunk_: 0 likely n_samples/chunk (p=1.00000): 12 likely sample_sizes (p=1.00000): 11264 n_mutual_patterns: 1 rtmd_avc1 (5->0) [1] (928) 1.000 100 00000000 00000000 00000000 00000000 00000002 09100000 00110600 0d80____ rtmd_ipcm (5->1) [0] (0) rtmd_ipcm (5->2) [0] (0) rtmd_ipcm (5->3) [0] (0) rtmd_ipcm (5->4) [0] (0) ss: max_part_size_: 23653057 fallback: 1 calling findMdat on truncated file.. Warning: no mp4-structure found in: '20240626_FX3_0081.mp4' Info: using orig_mdat_start_ (=262128) Info: reading mdat from truncated file ... Info: using 64-bit offsets for the broken file avc1: failed for no particular reason wouldMatch(8, skip="", force_strict=0, very_first=0) -> no // last_idx (-1) < 0 (reading element from mdat) Offset: 0x0 / 0x3fff8 : edb01312 250d81de trying chunkPredict first.. called getChunkPrediction(0x0 / 0x3fff8) ... chunk derived from track_order_: 'rtmd' (11264 x12) edb01312 250d81de e822d35f b41ef900 fd9319d1 ef6cc879 ec71d0e7 61e5d878 00000000 00000000 00000000 00000000 001c0100 09712273 f0010010 2000____ anyPatternMatchesHalf does not agree c.track_idx_=5 used stepToNextOwnChunkAbs of likelyNextTrack -> 0 (reading element from mdat) Offset: 0x21000 / 0x60ff8 : 57897b07 92da8a69 trying chunkPredict first.. called getChunkPrediction(0x21000 / 0x60ff8) ... Next track 'avc1' has unpredictable chunks Track codec: avc1 avc1: failed for no particular reason Track codec: ipcm Track codec: ipcm Track codec: ipcm Track codec: ipcm Track codec: rtmd unknown sequence -> enabling noise buffer .. [[ 2216384276 bytes omitted, next 2002 bytes were buffered ]] ...... [a lot of content omitted] (reading element from mdat) Offset: 0xcf45eea / 0xcf85ee2 : 59bf411c 1c97180b Track codec: avc1 avc1: no match because of nal type: 28 Track codec: ipcm Track codec: ipcm Track codec: ipcm Track codec: ipcm Track codec: rtmd called getChunkPrediction(0xcf45eea / 0xcf85ee2) ... src/mp4.h:298: bool Mp4::currentChunkFinished(int): Assertion `cur_track_idx == last_track_idx_' failed. ```
anthwlock commented 3 months ago

Hey - please send me the files (healthy+broken) and I might take a look at some point. You can upload them on wetransfer (2GB limit) or dropmefiles.net (100GB limit). If you think they are too big, you can use `untrunc -sh file.mp4`. In case you want to keep the link private, you can send me an email - see my Profile page. Thank you!

rattkin commented 1 month ago

I also have the same problem, can I help?

anthwlock commented 1 month ago

@rattkin Feel free to send me your files too - once I get around looking into this, they might be very helpful. If the files are large, please check if the bug is already reproducible with shortened files (untrunc -sh file.mp4). Thank you

rattkin commented 1 month ago

@anthwlock the shortened file works! Can I try restoring more than 500 mb? The entire file is 5516486208 bytes

anthwlock commented 1 month ago

@rattkin You can specify the size in MB as optional parameter, e.g. untrunc -sh file.mp4 1000 for the first 1GB. Feel free to send me the shortest file on which the problem is reproducible (plus the reference file)

rattkin commented 1 month ago

@anthwlock Thank you, it works, I get almost half of the video. Can I skip over the bad part and process the rest of the file? Rangle parameter? I tried it but can't get it working:

c:\tmp>untrunc.exe -range 20000:55000  working.mp4 broken.mp4
Info: version 'v367-13cafed' using ffmpeg '3.3.4' Lavc57.89.100
Info: reading working.mp4
Info: parsing healthy moov atom ...
Composition time offset atom found. Out of order samples possible.
Info: special track found (meta, 'Timed Metadata Media Handler')

Info: unknown track 'twos' found -> fallback to dynamic stats
Info: using dynamic stats, use '-is' to see them
Info: range: 20000:55000 -> 19992:55000 (35000)
Info: reading mdat from truncated file ...
Info: using 64-bit offsets for the broken file
Error: unable to find correct codec -> premature end (~0%)
       try '-s' to skip unknown sequences

Info: Found 0 packets ( avc1: 0 avc1-keyframes: 0 twos: 0 rtmd: 0 )
Info: Duration of avc1:  (0 ms)
Info: Duration of twos:  (0 ms)
Info: Duration of rtmd:  (0 ms)
Info: pruned empty 'avc1' track
Info: pruned empty 'twos' track
Info: pruned empty 'rtmd' track
Info: saving broken.mp4_fixed-dyn.mp4

c:\tmp>untrunc.exe -range 20000:55000 -s  working.mp4 broken.mp4
Info: version 'v367-13cafed' using ffmpeg '3.3.4' Lavc57.89.100
Info: reading working.mp4
Info: parsing healthy moov atom ...
Composition time offset atom found. Out of order samples possible.
Info: special track found (meta, 'Timed Metadata Media Handler')

Info: unknown track 'twos' found -> fallback to dynamic stats
Info: using dynamic stats, use '-is' to see them
Info: range: 20000:55000 -> 19992:55000 (35000)
Info: reading mdat from truncated file ...
Info: using 64-bit offsets for the broken file
src/mp4.cpp:2142: void Mp4::onFirstChunkFound(int): Assertion `next_chunk_idx_ == 0' failed.
anthwlock commented 1 month ago

The link shows me a 404 page. Please also include the reference file - Thank you

rattkin commented 1 month ago

The link shows me a 404 page. Please also include the reference file - Thank you

Sorry I had problems uploading. Here are the video files: https://mega.nz/file/wuoBzYxL#SLHx5wgxbXPsflEcarqbK1QzCuD-OFm6RcCTSzeFHWU https://mega.nz/file/Iu4TFCIQ#SHSkKdEguEj-NU8PSTwz_b_57qvKvHjF4BLAllmae_Y

flt6 commented 1 week ago

Same case, but for file 56G.

 untrunc -sh E:\BaiduNetdiskDownload\Fx3\C3952.MP4 1000
Info: version 'v367-13cafed' using ffmpeg '3.3.4' Lavc57.89.100
Info: 'E:\BaiduNetdiskDownload\Fx3\C3952.MP4' has invalid atom lenghts, see '-f'
28173
E:\Downloads\Compressed\untrunc_x64
λ untrunc -sh E:\BaiduNetdiskDownload\Fx3\C3951.MP4 1000
Info: version 'v367-13cafed' using ffmpeg '3.3.4' Lavc57.89.100
65536