เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

ดึงสถิติด้วย PyDub

การรู้ข้อมูล attribute ของไฟล์เสียงแต่ละไฟล์อย่างรวดเร็วจะเป็นประโยชน์มาก โดยเฉพาะเมื่อต้องการตรวจสอบจำนวนช่องสัญญาณ (channel) หรือ frame rate ว่าเหมาะสมสำหรับการถอดความหรือไม่

ในแบบฝึกหัดนี้ เราจะสร้างฟังก์ชัน show_pydub_stats() ที่รับชื่อไฟล์เสียงเป็น input จากนั้นนำเข้าไฟล์เสียงในรูปแบบ instance AudioSegment ของ PyDub แล้วแสดงข้อมูล attribute ต่าง ๆ เช่น จำนวนช่องสัญญาณ ความยาว และอื่น ๆ

ฟังก์ชันนี้จะ return instance AudioSegment กลับมาเพื่อนำไปใช้ต่อในภายหลัง

เราจะทดสอบฟังก์ชันกับไฟล์ .wav ที่แปลงไว้แล้ว ชื่อ call_1.wav

AudioSegment ถูก import จาก PyDub ไว้เรียบร้อยแล้ว

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Spoken Language Processing ด้วย Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • สร้าง instance AudioSegment ชื่อ audio_segment โดย import ไฟล์จาก parameter filename
  • แสดงจำนวนช่องสัญญาณโดยใช้ attribute channels
  • Return ตัวแปร audio_segment
  • ทดสอบฟังก์ชันด้วย "call_1.wav"

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

def show_pydub_stats(filename):
  """Returns different audio attributes related to an audio file."""
  # Create AudioSegment instance
  ____ = AudioSegment.from_file(____)
  
  # Print audio attributes and return AudioSegment instance
  print(f"Channels: {audio_segment.____}")
  print(f"Sample width: {audio_segment.sample_width}")
  print(f"Frame rate (sample rate): {audio_segment.frame_rate}")
  print(f"Frame width: {audio_segment.frame_width}")
  print(f"Length (ms): {len(audio_segment)}")
  return ____

# Try the function
call_1_audio_segment = ____(____)
แก้ไขและรันโค้ด