Jump to content
Sebaorgo

citirea lungimii unui fisier wav in python

Recommended Posts

Posted

Salut

 

am nevoie de ajutor in python. 

problema se pune in felul urmator :

am un folder cu mai multe fisiere audio de tip wav sau mp3 

vreau sa fac o lista cu numele fisierelor si lungimea lor luate din proprietati, nu prin calcule, este posibil in python ? 

 

multumesc in avans !

  • Active Members
Posted (edited)
"""
Python version: > 3.6
"""

import os


DIR_PATH = '/path/to/your/music/folder'


def print_files_info():
    for file in os.listdir(DIR_PATH):
        file_path = os.path.join(DIR_PATH, file)
        file_size = os.path.getsize(file_path)
        print(f'Name: {file}; Size: {file_size} bytes.')


if __name__ == '__main__':
    print_files_info()

 

Asta de mai sus iti afiseasa numele si marimea unui fisier. Daca vrei sa afisezi lungimea melodiei:

 

- pentru fisiere .mp3, poti folosi mutagen:

- pentru fisiere .wav, poti folosi wave si contextlib:

 

 

import contextlib
import os
import wave

from mutagen.mp3 import MP3



DIR_PATH = '/path/to/your/music/folder'


def print_files_info():
    for file in os.listdir(DIR_PATH):
        file_path = os.path.join(DIR_PATH, file)
        file_size = os.path.getsize(file_path)

        if file.endswith('.mp3'):
            audio = MP3(file_path)
            print(f'Name: {file}; Size: {file_size} bytes; Type: mp3; Length: {audio.info.length}')
            
        elif file.endswith('.wav'):
            with contextlib.closing(wave.open(file_path, 'r')) as f:
                # The length of an audio or wave file is determined by its framerate
                frames = f.getnframes()
                rate = f.getframerate()
                length = frames / float(rate)
                print(f'Name: {file}; Size: {file_size} bytes; Type: wav; Length: {length}')
                
        else:
            print('Pula .mp3 sau .wav')


if __name__ == '__main__':
    print_files_info()

 

Edited by MrGrj
Posted (edited)

Mai am o intrebare : cum pot sa dau precizia fisierelor wav in milisecunde ? Momentan e "prea exact"

de ex : 

length = 292.33922902494334

si eu vreau : 

length = 292.339

 

Multumesc anticipat ! 

 

 

EDIT :

Am reusit dar nu mai pot sterge postarea :D

Edited by Sebaorgo

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...