├── Decode.py ├── Encode.py ├── README.md └── Sample └── funny_guy.png /Decode.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy.fftpack, os, subprocess 3 | from pydub import AudioSegment 4 | 5 | import sys 6 | 7 | if sys.argv[1] == None: 8 | raise ValueError("No argument supplied") 9 | 10 | file = sys.argv[1] 11 | duration = 0.1 12 | timestamp = 0 13 | 14 | filename, ext_audio = os.path.splitext(file) 15 | retrieved_extension="" 16 | extension_lenght = 1 17 | 18 | 19 | if ext_audio !=".wav" and ext_audio !=".flac": 20 | print("converting audio file to wav") 21 | output_file = filename+".wav" 22 | 23 | 24 | command = [ 25 | 'ffmpeg', 26 | '-i', file, 27 | '-b:a', "8k", 28 | '-bitexact', 29 | '-acodec', 'pcm_s16le', 30 | '-ac', '1', 31 | '-ar', "8k", 32 | output_file 33 | ] 34 | 35 | subprocess.run(command, check=True) 36 | os.remove(file) 37 | print("BAAM deleted the old audio file and you didn't even saw") 38 | file = filename+".wav" 39 | 40 | print("Opening your file, It's gonna take like 2 minutes dude chill....") 41 | sound = AudioSegment.from_file(file, format="wav") 42 | sample_rate = sound.frame_rate 43 | audio_data = np.array(sound.get_array_of_samples()) 44 | audio_data = audio_data / np.max(np.abs(audio_data)) 45 | 46 | #get frequency 47 | print("Analyzing frequencies...") 48 | with open(filename, "wb+") as f: 49 | while True: 50 | 51 | start_sample = int(timestamp * sample_rate) 52 | end_sample = start_sample + int(duration * sample_rate) 53 | 54 | segment = audio_data[start_sample:end_sample] 55 | 56 | if len(segment) == 1 or len(segment) == 0: 57 | break 58 | 59 | 60 | N = len(segment) 61 | T = 1.0 / sample_rate 62 | yf = scipy.fftpack.fft(segment) 63 | xf = np.linspace(0.0, 1.0/(2.0*T), N//2) 64 | 65 | idx = np.argmax(np.abs(yf[:N//2])) 66 | freq = xf[idx] 67 | 68 | f.write(bytes([int((freq-100)/10)])) 69 | 70 | timestamp += duration 71 | f.close() 72 | #find the right extension of the file 73 | with open(filename, "r+b") as f: 74 | filesize = os.path.getsize(filename) 75 | while True: 76 | f.seek(filesize - extension_lenght) 77 | chunk = f.read(extension_lenght) 78 | if chunk.startswith(".".encode('ascii')): 79 | retrieved_extension = chunk.decode('ascii') 80 | f.truncate(filesize - extension_lenght) 81 | break 82 | extension_lenght+=1 83 | f.close() 84 | #write file 85 | num_of_file = 0 86 | while True: 87 | try: 88 | num_of_file +=1 89 | os.rename(filename, filename +str(num_of_file)+ retrieved_extension) 90 | break 91 | except FileExistsError: 92 | continue 93 | -------------------------------------------------------------------------------- /Encode.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import wave, os.path 3 | import sys 4 | 5 | if sys.argv[1] == None: 6 | raise ValueError("No argument supplied") 7 | 8 | frequencies = [] 9 | try: 10 | file = sys.argv[1] 11 | except: 12 | file = input("Enter file path: ") 13 | 14 | filename, ext = os.path.splitext(file) 15 | ext_byte = bytes(ext, "ascii") 16 | 17 | def generate_frequency(frequency, duration, sample_rate=7500 , amplitude=0.5): 18 | # generate the sound wave 19 | t = np.linspace(0, duration, int(sample_rate * duration), False) 20 | wave = amplitude * np.sin(2 * np.pi * frequency * t) 21 | 22 | frequencies.append(wave) 23 | 24 | 25 | with open(filename + ext, "rb") as f: 26 | print("Generating sound...") 27 | byte = f.read() 28 | #generate frequency based on the bytes of the file 29 | for i in range(len(byte)): 30 | generate_frequency(((byte[i]*10)+100), 0.1) 31 | #generate frequency for extension of file 32 | for i in range(len(ext)): 33 | generate_frequency((ext_byte[i]*10)+100, 0.1) 34 | 35 | 36 | #write file 37 | with wave.open(filename+".wav", 'w') as wf: 38 | print("Writing file...") 39 | wf.setnchannels(1) 40 | wf.setsampwidth(2) 41 | wf.setframerate(7500) 42 | 43 | for i in range(len(frequencies)): 44 | 45 | wave_data = (frequencies[i] * 32767).astype(np.int16) 46 | wf.writeframes(wave_data.tobytes()) 47 | 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # File-to-audio 2 | **WARNING: Don't try opening the audio files that you get, because your pc might crash and don't encode files larger than 1MB. This is not for storing your files, but a fun proof of concept.** 3 | 4 | # How to use 5 | First, clone the repo 6 | ```bash 7 | git clone https://github.com/D408-bot/File-to-audio.git 8 | ``` 9 | 10 | To run, use the following command 11 | ```bash 12 | python3 Encode.py 13 | ``` 14 | or 15 | ```bash 16 | python Encode.py 17 | ``` 18 | 19 | Same as for the decode 20 | -------------------------------------------------------------------------------- /Sample/funny_guy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D408-bot/File-to-audio/c7bf777c898c6eff3a369f53fb8e94d48701c726/Sample/funny_guy.png --------------------------------------------------------------------------------