anthwlock / untrunc

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

Error: unable to find correct codec -> premature end (avc1) #84

Open anlag opened 3 years ago

anlag commented 3 years ago

I'm running into problems trying to fix a fairly long video file, a screen recording where the laptop froze and corrupted the output file. Fortunately I have a healthy recording from earlier in the same day, and so I was very pleased to find this repo, hoping it might help me rescue the broken video.

I'm using the latest master version, via docker like so:

Info: version 'b150ae5-dirty' using ffmpeg '3.4.8-0ubuntu0.2'
Info: reading /mnt/working.mp4
Info: parsing healthy moov atom ... 
Composition time offset atom found. Out of order samples possible.

Info: reading mdat from truncated file ...
Info: Muted ffmpeg to reduce redundant warnings/errors. Use '-do' to see them.
Error: unable to find correct codec -> premature end (~0.003558%)
       try '-s' to skip unknown sequences

Warning: guessed frame durations of 'mp4a' will probably be wrong!
Info: Found 2 packets ( mp4a: 0 avc1: 2 avc1-keyframes: 1 )
Info: Duration of mp4a:  (0 ms)
Info: Duration of avc1: 66ms  (66 ms)
Info: pruned empty 'mp4a' track
Info: saving /mnt/broken.mp4_fixed.mp4

With verbose output:

[anlag@R90KMF56 untrunc-anthwlock]$ sudo docker run -v ~/Videos/Recordings/videofix:/mnt untrunc -v /mnt/working.mp4 /mnt/broken.mp4
[sudo] password for anlag: 
Composition time offset atom found. Out of order samples possible.
Info: version 'b150ae5-dirty' using ffmpeg '3.4.8-0ubuntu0.2'
Info: reading /mnt/working.mp4
Info: parsing healthy moov atom ... 
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/mnt/working.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 03:01:15.20, start: -0.002667, bitrate: 1206 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 1116 kb/s, 23.10 fps, 30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: vorbis (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 81 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
ftyp_ = 'isom'
found avcC after: 102
remaining len:67
parsing avcC ...
len_sps: 28
decoding SPS ...
log2_max_frame_num: 4
avcC got decoded
ss: avc1 is_stable: 0
ss: using f=7 span
ss: mp4a is_stable: 0
ss: using f=7 span

ss: reset to default (from 1990777 ~= 0.24*default)
ss: max_part_size_: 8388608
calling findMdat on truncated file..
Info: reading mdat from truncated file ...

(reading element from mdat)
Offset: 0 / 48 : 000002a9 0605ffff
Track codec: mp4a
Failure because of NULL header
Track codec: avc1
avc1: Match with 0 header
sps_info (before): 1 4 5 0
sps_info (after):  1 4 5 0
---
Length: 681+4
Ref idc: 0
Nal type: 6
Partial avc1-length: 685
---
Length: 58207+4
Ref idc: 3
Nal type: 5
Partial avc1-length: 58896
---
Length: 919+4
Ref idc: 2
Nal type: 1
Different frame number
part-length: 58896
1th sample in 1th avc1-chunk

(reading element from mdat)
Offset: 58896 / 58944 : 00000397 419a2418
Track codec: mp4a
Failure because of NULL header
Track codec: avc1
avc1: Match with 0 header
---
Length: 919+4
Ref idc: 2
Nal type: 1
Partial avc1-length: 923
---
Length: 0+4
Warning: Forbidden first bit 1
failed parsing nal-header
part-length: 923
2th sample in 1th avc1-chunk
skipping zeros at: 59819 / 59867

(reading element from mdat)
Offset: 59823 / 59871 : b8419e42 424f0c85
Track codec: mp4a
Success for no particular reason....
Info: Muted ffmpeg to reduce redundant warnings/errors. Use '-do' to see them.
nb_samples: 0
got_frame: 0
channels: 2, 0
part-length: 524288
Codec::was_bad_ = 1 -> skipping
Track codec: avc1
avc1: failed for no particular reason
Error: unable to find correct codec -> premature end (~0.003558%)
       try '-s' to skip unknown sequences

mdat->file_end: 59871
Warning: guessed frame durations of 'mp4a' will probably be wrong!
Info: Found 2 packets ( mp4a: 0 avc1: 2 avc1-keyframes: 1 )
Info: Duration of mp4a:  (0 ms)
first_failed: 3 of 249300
order: (1, 1024) (1, 2560) 
reduced origt_ctts_: 249300 -> 0
ctts values seem unpredictable..
Info: Duration of avc1: 66ms  (66 ms)
Info: pruned empty 'mp4a' track
Info: saving /mnt/broken.mp4_fixed.mp4

The error is the same as in this issue: https://github.com/anthwlock/untrunc/issues/67

I suppose it's possible I also have an unusual "byte stream format" but I'm not sure how to find out. If that information is in the output of mediainfo then I don't know what to look for.

I'm happy to upload the files and share them privately to help troubleshooting.

anthwlock commented 3 years ago

Codec::wasbad = 1 -> skipping

I think this is the problematic line. It also appeared in #83. So please try again with the newest changes. It may be important to use ffmepg <= 3.4 in this case (it was with #83). Since you are using docker, this should already be the case.

It would be interesting to know whether the recovery works with ffmepg >= 3.4. If you test this let me know. If you send me the files, I could test this too (and maybe investigate further). In that case the first 200mb (of both files) should suffice, which can be extracted via untrunc -sh <file.mp4>. You could then upload these to https://wetransfer.com/ and send me the link via email.

anlag commented 3 years ago

Thanks for the suggestions and apologies for the late reply.

I grabbed the latest changes to the repo and rebuilt the image to use ffmpeg 3.3.9. It still fails right away, though the output is now 1.6 MB rather than only 60 KB, and the error is slightly different, notably:

Invalid length: part-length is -109499552

Full output below. I'll arrange to send you the files.

[anlag@R90KMF56 untrunc-anthwlock]$ sudo docker run -v ~/Videos/Recordings/videofix:/mnt untrunc -v /mnt/working.mp4 /mnt/broken.mp4
Info: version '3cf5947-dirty' using ffmpeg '3.3.9'
Info: reading /mnt/working.mp4
Info: parsing healthy moov atom ... 
Composition time offset atom found. Out of order samples possible.
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/mnt/working.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 03:01:15.20, start: -0.002667, bitrate: 1206 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 1116 kb/s, 23.10 fps, 30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: vorbis (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 81 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
[vorbis @ 0x561aed467680] Not a Vorbis I audio packet.
ftyp_ = 'isom'
found avcC after: 102
remaining len:67
parsing avcC ...
len_sps: 28
decoding SPS ...
log2_max_frame_num: 4
avcC got decoded
ss: avc1 is_stable: 0
ss: using f=7 span
ss: mp4a is_stable: 0
ss: using f=7 span

ss: reset to default (from 1990777 ~= 0.24*default)
ss: max_part_size_: 8388608
calling findMdat on truncated file..
Info: reading mdat from truncated file ...

(reading element from mdat)
Offset: 0 / 48 : 000002a9 0605ffff
Track codec: mp4aInvalid length: part-length is -109499552
Failure because of NULL header
Track codec: avc1
avc1: Match with 0 header
sps_info (before): 1 4 5 0
sps_info (after):  1 4 5 0
---
Length: 681+4
Ref idc: 0
Nal type: 6
Partial avc1-length: 685
---
Length: 58207+4
Ref idc: 3
Nal type: 5
Partial avc1-length: 58896
---
Length: 919+4
Ref idc: 2
Nal type: 1
Different frame number
part-length: 58896
1th sample in 1th avc1-chunk

(reading element from mdat)
Offset: 58896 / 58944 : 00000397 419a2418
Track codec: mp4a
Failure because of NULL header
Track codec: avc1
avc1: Match with 0 header
---
Length: 919+4
Ref idc: 2
Nal type: 1
Partial avc1-length: 923
---
Length: 0+4
Warning: Forbidden first bit 1
failed parsing nal-header
part-length: 923
2th sample in 1th avc1-chunk
skipping zeros at: 59819 / 59867

(reading element from mdat)
Offset: 59823 / 59871 : b8419e42 424f0c85
Track codec: mp4a
Success for no particular reason....
nb_samples: 0
got_frame: 0
channels: 2, 0
part-length: 524288
Warning: Codec::was_bad_ = 1
1th sample in 1th mp4a-chunk

(reading element from mdat)
Offset: 584111 / 584159 : 9a2519f5 f04fe201
Track codec: mp4a
Success for no particular reason....
nb_samples: 576
part-length: 524288
2th sample in 1th mp4a-chunk

(reading element from mdat)
Offset: 1108399 / 1108447 : 245ce212 dcdf8e9b
Track codec: mp4a
mp4a: Success because of large s value
nb_samples: 576
part-length: 524288
3th sample in 1th mp4a-chunk

(reading element from mdat)
Offset: 1632687 / 1632735 : cf61aa6a 538b9791
Track codec: mp4a
Success for no particular reason....
nb_samples: 0
got_frame: 0
channels: 2, 0
part-length: -1094995529
Invalid length: part-length is -1094995529
Track codec: avc1
avc1: failed for no particular reason
Error: unable to find correct codec -> premature end (~0.0971%)
       try '-s' to skip unknown sequences

mdat->file_end: 1632735
Warning: only found decode times partially!Info: Found 5 packets ( mp4a: 3 avc1: 2 avc1-keyframes: 1 )
Tip: Audio and video seem to have different durations (0.5455).
     If audio and video are not in sync, give `-sv` a try. See `--help`
Info: Duration of mp4a: 36ms  (36 ms)
first_failed: 3 of 249300
order: (1, 1024) (1, 2560) 
reduced origt_ctts_: 249300 -> 0
ctts values seem unpredictable..
Info: Duration of avc1: 66ms  (66 ms)
Info: saving /mnt/broken.mp4_fixed.mp4
anlag commented 3 years ago

Hi again, I was revisiting this (since my wife keeps asking me for it, it's her recording) and realized while I had tried with ffmpeg v3.4 in the older code version (b150ae5) I had not done so with the latest (3cf5947.) So output from that below, unfortunately it's still unable to find the correct codec.

I think you received the files I sent previously already? If there's anything else I can do to facilitate or try myself, let me know and I'd be happy to give it a go. Many thanks!

[anlag@R90KMF56 untrunc-anthwlock]$ sudo docker run -v ~/Videos/Recordings/videofix:/mnt untrunc -v /mnt/working.mp4 /mnt/broken.mp4
Composition time offset atom found. Out of order samples possible.
Info: version '3cf5947-dirty' using ffmpeg '3.4.8-0ubuntu0.2'
Info: reading /mnt/working.mp4
Info: parsing healthy moov atom ... 
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/mnt/working.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 03:01:15.20, start: -0.002667, bitrate: 1206 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 1116 kb/s, 23.10 fps, 30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: vorbis (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 81 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
ftyp_ = 'isom'
found avcC after: 102
remaining len:67
parsing avcC ...
len_sps: 28
decoding SPS ...
log2_max_frame_num: 4
avcC got decoded
ss: avc1 is_stable: 0
ss: using f=7 span
ss: mp4a is_stable: 0
ss: using f=7 span

ss: reset to default (from 1990777 ~= 0.24*default)
ss: max_part_size_: 8388608
calling findMdat on truncated file..
Info: reading mdat from truncated file ...

(reading element from mdat)
Offset: 0 / 48 : 000002a9 0605ffff
Track codec: mp4a
Failure because of NULL header
Track codec: avc1
avc1: Match with 0 header
sps_info (before): 1 4 5 0
sps_info (after):  1 4 5 0
---
Length: 681+4
Ref idc: 0
Nal type: 6
Partial avc1-length: 685
---
Length: 58207+4
Ref idc: 3
Nal type: 5
Partial avc1-length: 58896
---
Length: 919+4
Ref idc: 2
Nal type: 1
Different frame number
part-length: 58896
1th sample in 1th avc1-chunk

(reading element from mdat)
Offset: 58896 / 58944 : 00000397 419a2418
Track codec: mp4a
Failure because of NULL header
Track codec: avc1
avc1: Match with 0 header
---
Length: 919+4
Ref idc: 2
Nal type: 1
Partial avc1-length: 923
---
Length: 0+4
Warning: Forbidden first bit 1
failed parsing nal-header
part-length: 923
2th sample in 1th avc1-chunk
skipping zeros at: 59819 / 59867

(reading element from mdat)
Offset: 59823 / 59871 : b8419e42 424f0c85
Track codec: mp4a
Success for no particular reason....
Info: Muted ffmpeg to reduce redundant warnings/errors. Use '-do' to see them.
nb_samples: 0
got_frame: 0
channels: 2, 0
part-length: 524288
Warning: Codec::was_bad_ = 1
1th sample in 1th mp4a-chunk

(reading element from mdat)
Offset: 584111 / 584159 : 9a2519f5 f04fe201
Track codec: mp4a
Success for no particular reason....
nb_samples: 576
part-length: 524288
2th sample in 1th mp4a-chunk

(reading element from mdat)
Offset: 1108399 / 1108447 : 245ce212 dcdf8e9b
Track codec: mp4a
mp4a: Success because of large s value
nb_samples: 576
part-length: 524288
3th sample in 1th mp4a-chunk

(reading element from mdat)
Offset: 1632687 / 1632735 : cf61aa6a 538b9791
Track codec: mp4a
Success for no particular reason....
nb_samples: 576
got_frame: 0
channels: 2, 2
part-length: -1094995529
Invalid length: part-length is -1094995529
Track codec: avc1
avc1: failed for no particular reason
Error: unable to find correct codec -> premature end (~0.0971%)
       try '-s' to skip unknown sequences

mdat->file_end: 1632735
Warning: only found decode times partially!Info: Found 5 packets ( mp4a: 3 avc1: 2 avc1-keyframes: 1 )
Tip: Audio and video seem to have different durations (0.5455).
     If audio and video are not in sync, give `-sv` a try. See `--help`
Info: Duration of mp4a: 36ms  (36 ms)
first_failed: 3 of 249300
order: (1, 1024) (1, 2560) 
reduced origt_ctts_: 249300 -> 0
ctts values seem unpredictable..
Info: Duration of avc1: 66ms  (66 ms)
Info: saving /mnt/broken.mp4_fixed.mp4