Created
January 9, 2018 01:12
-
-
Save brunetton/d3c2398ba064f087f76114b320f658bf to your computer and use it in GitHub Desktop.
Truncated waves files after photorec restore from Zoom h2n in 4ch mode - correction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Tris *experimental* program tries to "re-hash" waves files in "good order" after deletion then recover with photorec | |
on a FAT SD card; recorded in 4 chanels mode with Zoom H2n. | |
Does the job, but probably not compatible with other situations. | |
""" | |
import sys | |
import os | |
import glob | |
from wavefile import WaveReader, WaveWriter | |
CHUNK_SIZE = 24576 # found "by hand", searching for buggy zones easy to identify and mesure | |
def convert_file(in_filename, out_filename): | |
if os.path.exists(out_filename): | |
os.remove(out_filename) | |
with WaveReader(in_filename) as r: | |
with WaveWriter( | |
out_filename, | |
channels=r.channels, | |
samplerate=r.samplerate, | |
) as w: | |
a = True | |
r.read_iter() | |
for data in r.read_iter(size=CHUNK_SIZE): | |
if a: | |
w.write(data) | |
a = not a | |
if len(sys.argv) < 2: | |
print("input dir nedded") | |
sys.exit(-1) | |
input_dir = sys.argv[1] | |
assert os.path.exists(input_dir), "not found: {!r}".format(input_dir) | |
out_dirname = os.path.join(input_dir, "out") | |
if not os.path.exists(out_dirname): | |
os.mkdir(out_dirname) | |
for filename in sorted(glob.glob(os.path.join(input_dir, "*.wav"))): | |
print(filename) | |
convert_file(filename, os.path.join(out_dirname, os.path.basename(filename))) | |
print("end") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment