Архив метки: chunk

Python заготовка для создания wav-файлов без дополнительных модулей

"""Create wav file."""
import os
import sys

file_name = input("WAV file name: ")
if os.path.exists(file_name):
    print("ERROR: File name is busy")
    sys.exit(1)
with open(file_name, "wb") as wav_file:
    wav_file.write(
        bytes(
            (
                0x52, 0x49, 0x46, 0x46,  # RIFF
                # 0x24, 0x08, 0x00, 0x00,  # Chunk size = 2084
                0xf4, 0x08, 0x00, 0x00,  # Chunk size = ???
                0x57, 0x41, 0x56, 0x45,  # WAVE
                0x66, 0x6d, 0x74, 0x20,  # fmt
                0x10, 0x00, 0x00, 0x00,  # Subchunk1 size = 16
                0x01, 0x00,  # Audio Format = 1 (PCM)
                0x02, 0x00,  # Num Channels = 2
                # 0x01, 0x00,  # Num Channels = 1
                0x22, 0x56, 0x00, 0x00,  # Sample Rate = 22050
                0x88, 0x58, 0x01, 0x00,  # Byte Rate = 88200
                0x04, 0x00,  # Block Align = 4
                0x10, 0x00,  # Bits per sample = 16
                0x64, 0x61, 0x74, 0x61,  # data
                # 0x00, 0x08, 0x00, 0x00,  # subchunk size = 2048
                0x00, 0xf8, 0x00, 0x00,  # subchunk size = ???

                0x00, 0x00,  # sample 1 left
                0x00, 0x00,  # sample 1 right

                0x24, 0x17,  # sample 2 left
                0x1e, 0xf3,  # sample 2 right

                0x3c, 0x13,  # sample 3 left
                0x3c, 0x14,  # sample 3 right

                0x16, 0xf9,
                0x18, 0xf9,

                0x34, 0xe7,
                0x23, 0xa6,

                0x3c, 0xf2,
                0x24, 0xf2,

                0x11, 0xce,
                0x1a, 0x0d,
            )
        )
    )
    for _ in range(1000000):
        wav_file.write(
            bytes(
                (
                    0x16, 0xf9,
                    0x18, 0xf9,

                    0x34, 0xe7,
                    0x23, 0xa6,

                    0x3c, 0xf2,
                    0x24, 0xf2,

                    0x11, 0xce,
                    0x1a, 0x0d,
                )
            )
        )

Python скрипт для сбора информации по файлам в папке

# -*- coding: utf-8 -*-

'''
на входе папка или файл, на выходе CSV: имя файла;сумма мд5;папка полностью;имя компа
'''

import os
import hashlib
import platform
import time
import sys

class MyItem:
  def __init__(self, f):
    self.hostname = hostname
    self.md5sum = md5(f)
    self.size = os.path.getsize(f)
    self.created = "%s" % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getctime(f)))
    self.modified = "%s" % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getmtime(f)))
    self.ext = os.path.splitext(f)[1]
    self.full_dir, self.file_name = os.path.split(os.path.abspath(f))

def md5(fname):  # https://stackoverflow.com/questions/3431825/generating-an-md5-checksum-of-a-file
    hash_md5 = hashlib.md5()
    with open(fname, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hash_md5.update(chunk)
    return hash_md5.hexdigest()

hostname = platform.node()

result_header = "file_name;md5sum;size;created;modified;ext;full_path;hostname\n"
result_file = open('./result.csv','w')
result_file.write(result_header)

targets = []

if len(sys.argv) > 1:
  targets = sys.argv[1::]
else:
  targets.append(".")

for t in targets:
  if os.path.exists(t):
    if os.path.isdir(t):
      for current_dir, dirs, files in os.walk(t):
        for f in files:
          try:
            i = os.path.join(current_dir, f)
            print(i)
            item = MyItem(i)
            result_file.write('%s;%s;%s;%s;%s;%s;%s;%s\n'%(item.file_name, item.md5sum, item.size, item.created, item.modified, item.ext, item.full_dir, item.hostname))
          except:
            print('Error!')
    if os.path.isfile(t):
      try:
        i = t
        print(i)
        item = MyItem(i)
        result_file.write('%s;%s;%s;%s;%s;%s;%s;%s\n'%(item.file_name, item.md5sum, item.size, item.created, item.modified, item.ext, item.full_dir, item.hostname))
      except:
        print('Error!')

result_file.close()