0

I want to feed audio channel of a video file to the following TenorFlow function:

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

Where Args:

  • contents: A Tensor of type string. The WAV-encoded audio, usually from a file.

  • desired_channels: An optional int. Defaults to -1. Number of sample channels wanted.

  • desired_samples: An optional int. Defaults to -1. Length of audio requested.

  • name: A name for the operation (optional).

DevLoverUmar
  • 11,809
  • 11
  • 68
  • 98
  • 1
    Check those answers: [How does tf.audio.decode_wav get its contents?](https://stackoverflow.com/questions/58096095/how-does-tf-audio-decode-wav-get-its-contents) & [Python extract wav from video file](https://stackoverflow.com/questions/26741116/python-extract-wav-from-video-file). – Xarvalus Oct 10 '19 at 14:30

2 Answers2

1

You can extract the audio of video by eg.:

import subprocess

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

subprocess.call(command, shell=True)

And pass the *.wav file as tensor to tf.audio.decode_wav:

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

References:

Xarvalus
  • 2,873
  • 1
  • 19
  • 29
0

For windows 10, I solved my problem this way:

  1. I downloaded and installed ffmpeg for windows from here
  2. Extracted wav audio files from videos as:

    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. Decoded audio files to wavform as:

        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
            )
    
DevLoverUmar
  • 11,809
  • 11
  • 68
  • 98