import glob, sys, os
import subprocess


class AudioParser():
    def get_parse_streams(self,file_name):
        command = 'ffmpeg -i file:"%s"' % file_name
        p = subprocess.Popen(command, shell=True,
                     stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        output = p.communicate()[0]
        streams = output.split("Stream #")
        res = []

        english = ['English','eng' , 'Eng' , 'ENG']

        for stream in streams:
            for eng in english:
                if  eng in stream and 'Audio' in stream:
                    res = stream[0:3]
                    break

        if not res:
            for stream in streams:
                if 'Audio' in stream:
                    res.append(stream[0:3])
        return res


    def extractWavFile(self, file_name, stream, output_file):
        # -acodec pcm_s16le
        command = 'ffmpeg -i file:"%s" -map %s -acodec pcm_s16le -ac 2 %s.wav' % (file_name, stream,output_file)
        p = subprocess.Popen(command, shell=True,
                     stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

        output, errors = p.communicate()
        return output
