Ivan000 commited on
Commit
d3701bd
·
verified ·
1 Parent(s): 16f11e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -2
app.py CHANGED
@@ -15,12 +15,15 @@ def generate_frequency_visualization(audio_path):
15
  try:
16
  # Load the audio file
17
  y, sr = librosa.load(audio_path, sr=None)
 
18
 
19
  if sr == 0 or len(y) == 0:
20
  raise ValueError("Invalid audio file: sampling rate or audio data is zero.")
21
 
22
  # Perform Short-Time Fourier Transform (STFT)
23
- D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max)
 
 
24
 
25
  # Create a directory to save the frames
26
  os.makedirs('frames', exist_ok=True)
@@ -28,11 +31,12 @@ def generate_frequency_visualization(audio_path):
28
  # Generate and save each frame
29
  for i, frame in enumerate(D.T):
30
  plt.figure(figsize=(10, 6))
31
- librosa.display.specshow(frame.reshape(1, -1), sr=sr, x_axis='time', y_axis='log')
32
  plt.axis('off')
33
  plt.savefig(f'frames/frame_{i:04d}.png', bbox_inches='tight', pad_inches=0)
34
  plt.close()
35
 
 
36
  return 'frames'
37
  except Exception as e:
38
  print(f"Error generating frequency visualization: {e}")
@@ -68,6 +72,7 @@ def create_video_from_frames(frames_directory):
68
  video_path = 'output_video.mp4'
69
  clip.write_videofile(video_path, codec='libx264')
70
 
 
71
  return video_path
72
  except Exception as e:
73
  print(f"Error creating video from frames: {e}")
 
15
  try:
16
  # Load the audio file
17
  y, sr = librosa.load(audio_path, sr=None)
18
+ print(f"Loaded audio file with sampling rate: {sr}, and duration: {librosa.get_duration(y=y, sr=sr)} seconds.")
19
 
20
  if sr == 0 or len(y) == 0:
21
  raise ValueError("Invalid audio file: sampling rate or audio data is zero.")
22
 
23
  # Perform Short-Time Fourier Transform (STFT)
24
+ n_fft = 2048 # Ensure n_fft is set to a valid number
25
+ hop_length = 512 # Ensure hop_length is set to a valid number
26
+ D = librosa.amplitude_to_db(np.abs(librosa.stft(y, n_fft=n_fft, hop_length=hop_length)), ref=np.max)
27
 
28
  # Create a directory to save the frames
29
  os.makedirs('frames', exist_ok=True)
 
31
  # Generate and save each frame
32
  for i, frame in enumerate(D.T):
33
  plt.figure(figsize=(10, 6))
34
+ librosa.display.specshow(frame.reshape(1, -1), sr=sr, x_axis='time', y_axis='log', hop_length=hop_length)
35
  plt.axis('off')
36
  plt.savefig(f'frames/frame_{i:04d}.png', bbox_inches='tight', pad_inches=0)
37
  plt.close()
38
 
39
+ print(f"Generated {len(D.T)} frames for visualization.")
40
  return 'frames'
41
  except Exception as e:
42
  print(f"Error generating frequency visualization: {e}")
 
72
  video_path = 'output_video.mp4'
73
  clip.write_videofile(video_path, codec='libx264')
74
 
75
+ print(f"Video created with {len(frame_files)} frames.")
76
  return video_path
77
  except Exception as e:
78
  print(f"Error creating video from frames: {e}")