【问题标题】:Extracting Audio channel from a video file to feed in decode_wav function of TensorFlow从视频文件中提取音频通道以输入 TensorFlow 的 decode_wav 函数
【发布时间】:2020-02-07 10:13:43
【问题描述】:

我想将视频文件的音频通道提供给以下 TenorFlow 函数:

tf.audio.decode_wav(
contents,
desired_channels=-1,
desired_samples=-1,
name=None)

其中的参数:

  • contents:字符串类型的张量。 WAV 编码的音频,通常 从一个文件。

  • desired_channels:一个可选的整数。默认为 -1。样品数量 想要的频道。

  • desired_samples:一个可选的整数。默认为 -1。音频长度 请求。

  • name:操作的名称(可选)。

【问题讨论】:

标签: python tensorflow wav


【解决方案1】:

您可以通过以下方式提取视频的音频:

import subprocess

command = "ffmpeg -i C:/test.mp4 -ab 160k -ac 2 -ar 44100 -vn audio.wav"

subprocess.call(command, shell=True)

并将*.wav 文件作为张量传递给tf.audio.decode_wav

raw_audio = tf.io.read_file(filename)
waveform = tf.audio.decode_wav(raw_audio)

参考资料:

【讨论】:

    【解决方案2】:

    对于 Windows 10,我以这种方式解决了我的问题:

    1. 我下载并安装了 ffmpeg for windows from here
    2. 从视频中提取的wav音频文件为:

      import os,shlex, subprocess
      files = os.listdir('.')
      #convert all video files of the current directory to wav audio files
      for f in files:
          if(f.endswith('.wmv') or f.endswith('.mp4') or f.endswith('.avi')):
              command_line = "ffmpeg -i "+f+" -ab 160k -ac 2 -ar 44100 -vn "+f[0:-4]+".wav"
              #command_line = "ffmpeg -i default.wmv -ab 160k -ac 2 -ar 44100 -vn default.wav"
              args = shlex.split(command_line)
              print(args)
              p = subprocess.Popen(args) # Success!
              print(p)
      
    3. 将音频文件解码为波形:

          import tensorflow as tf
      files = os.listdir('.')
      #convert all video files of the current directory to wav audio files
      for filename in files:
          if(filename.endswith('.wav')):
              print(filename)
              audio_binary = tf.io.read_file(filename,name=None)
              waveform,_ = tf.audio.decode_wav(
                  audio_binary,
                  desired_channels=-1,
                  desired_samples=-1,
                  name=None
              )
      

    【讨论】:

      猜你喜欢
      • 2012-07-10
      • 2014-08-14
      • 1970-01-01
      • 2020-03-26
      • 2012-12-06
      • 2012-12-12
      • 2010-10-09
      相关资源
      最近更新 更多