3

I would like to know if it is possible, using Windows and c++, to take a large video file (several gigabytes in length) and delete the first and last few hundred megabytes of it “in-place”.

The traditional approach of copying the useful data to a new file often takes upwards of 20 minutes of seemingly needless copying.

Is there anything clever that can be done low-level with the disk to make this happen?

5 Answers5

6

Sure, it's possible in theory. But if your filesystem is NTFS, be prepared to spend a few months learning about all the data structures that you'll need to update. (All of which are officially undocumented BTW.)

Also, you'll need to either

  1. Somehow unmount the volume and make your changes then; or
  2. Learn how to write a kernel filesystem driver, buy a license from MS, develop the driver and use it to make changes to a live filesystem.

It's a bit easier if your filesystem is something simpler like FAT32. But either way: in short, it might be possible, but even if it is it'll take years out of your life. My advice: don't bother.

Instead, look at other ways you could solve the problem: e.g. by using an avisynth script to serve just the frames from the region you are interested in.

j_random_hacker
  • 50,331
  • 10
  • 105
  • 169
  • 1
    > officially undocumented BTW Sorry, what? http://technet.microsoft.com/en-us/library/cc781134.aspx – Mark Apr 11 '09 at 20:56
  • ...and if your filesystem is FAT32 then your files will be 4 GB or smaller anyway. :) – bk1e Apr 12 '09 at 17:35
  • @Mark: You may be right -- I just recall that back in around 2006 when I was looking for a free & reliable Linux NTFS driver supporting writing, the author of one of the available drivers complained that the spec details were not released. Wikipedia says much the same (though of course it may be out of date): http://en.wikipedia.org/wiki/NTFS#Interoperability. – j_random_hacker Feb 20 '12 at 02:15
4

Are you hoping to just fiddle around with sector addresses in the directory entry? It's virtually inconceivable that plan would work.

First of all, it would require that the amount of data you wish to delete be exactly a sector size. That's not very likely considering that there is probably some header data at the very start that must remain there.

Even if it mets those requirements, it would take a low-level modification, which Windows tries very hard to prevent you from doing.

James Curran
  • 101,701
  • 37
  • 181
  • 258
  • "Are you hoping to just fiddle around with sector addresses in the directory entry?" Actually, yes :) Although I don't know enough about disks to know if that's even possible, hence the post here. –  Apr 11 '09 at 20:22
2

Yes, you can do this, on NTFS.

The end you remove with SetFileLength.

The beginning, or any other large consecutive region of the file, you overwrite with zeros. You then mark the file "sparse", which allows the file system to reclaim those clusters.

Note that this won't actually change the offset of the data relative to the beginning of the file, it only prevents the filesystem from wasting space storing unneeded data.

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
2

Maybe your file format allows to 'skip' the bytes, so that you could simply write over (i.e. with memory mapping) the necessary parts. This would of course still use up unnecessarily much disk space.

akauppi
  • 17,018
  • 15
  • 95
  • 120
1

Even if low level filesystem operations were easy, editing a video file is not simply a matter of deleting unwanted megabytes. You still do have to consider concepts such as compression, frames, audio and video muxing, media file containers, and many others...

Your best solution is to simply accept your idle twenty minutes.

mouviciel
  • 66,855
  • 13
  • 106
  • 140