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

example working video - relevant properties #74

Closed Humphreybas closed 3 years ago

Humphreybas commented 3 years ago

It is clearly stated that:

You need both the broken video and an example working video (ideally from the same camera, if not the chances to fix it are slim).

However I unfortunately do not have a working example video. So I was wondering: how can I optimise my slim changes on success?

For example: what properties of the working video are relevant? codec? length? resolution? ...? I was thinking: then I can create such a file myself with ffmpeg. Maybe even multiple files to try them all if there are parameters that I am not sure off..

anthwlock commented 3 years ago

Most important is that the same tracks/codecs are present. But also information about how they are packed together is used, which is hard (impossible?) to simulate with ffmpeg.

I wonder: how do you know / want to find out which codecs are present? Do you expect both video and audio to be present? Any other data? In your case I'd first try out "random" reference files, before making a "hand crafted" one.

Humphreybas commented 3 years ago

Well I have no clue about the packing, so I should read up on that.. I expect both audio and video.

Part of my plan was to just select all plausible codecs and then try them all with a script. Python is my most native language for that so I would do something like this:

#!/usr/bin/env python3
import os
from subprocess import check_output

#check codecs
#codecs_raw = check_output('ffmpeg -codecs',shell=True)

video_codecs = ['h264','h263','av1','vp9','mpeg4']
audio_codecs = ['aac','flac','mp3','opus']

for vc in video_codecs:
    for ac in audio_codecs:
        text = 'refvideo_{}_{}'.format(vc,ac)

        #create audio file
        command = 'ffmpeg -f lavfi -i "sine=frequency=1000:duration=5" -ac 2 -c:a {} {}.{}'.format(ac,text,ac)
        check_output(command,shell=True)

        #create video file
        font = '/usr/share/fonts/truetype/freefont/FreeSans.ttf'
        command = 'ffmpeg -i {}.{} -f lavfi -i color=c=black:s=1280x720:d=3.9 -vf "drawtext=fontfile={}:fontsize=160:fontcolor=white:x=(w-text_w)/3:y=(h-text_h)/2:text=\'{}\'" -shortest -r 24 -c:v libx264 {}.mp4'.format(text,ac,font,text,text)
        check_output(command,shell=True)

        #remove audio file
        os.remove('{}.{}'.format(text,ac))
        #and then here I could call untrunc

(I will report back when I actually tried this)

Brainfart: could it be interesting for the untrunc project to have a library of real MP4 files coming from real camera's to use when you don't have a reference file?

anthwlock commented 3 years ago

Good idea, though ofc currently always libx264 is used. Btw, instead of nested for-loops you could use itertools.product. Also I'd add h265 to the video_codecs list.

The "packing" is essentially a implementation detail. For example, some encoders always pack 10 audio samples, then 30 video samples etc, then (again) 10 audio samples, and so on. Or maybe they add padding before/after a certain track t, so that t always start at a fileoffsets o, for which o % big_num == 0. It's also possible that samples of some track always start/end with the same pattern.

Characteristics like these may be used by untrunc, with the assumption that the broken file has the same characteristics. However, depending on the file, it might not matter that this assumption is/could be (partially) broken.

could it be interesting for the untrunc project to have a library of real MP4 files coming from real camera's to use when you don't have a reference file?

Definitively! E.g. for:

mplayer has such an archive of samples. But it would take some effort to extract, out of those 16188 samples, the ones which are useful for this project. Do you have another idea?

Humphreybas commented 3 years ago

Thanks for the itertools tip!

Wow that mplayer sample list is impressive, but indeed quite challenging to filter. We would need a sample database simply filled with mp4 sources. So mainly just camera/phone/etc. models I guess.. Not sure how to collect them except for just asking people to upload them somehow..