john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

command convert audio avconv

import subprocess
import time
import os


def listing_as_directories_and_files( start, exclude_extension ):
    directory_list = list()
    file_list = list()
    for current in os.listdir( start ):
        if( current[0] != '.' ):  # do not add hidden directories or hidden files
            fullPath = os.path.join( start , current )
            if( os.path.isdir( fullPath ) ):
                directory_list.append( current )
            if( os.path.isfile( fullPath ) ) and not current.endswith( exclude_extension ):
                file_list.append( current )                 # filename only, not the whole path
    return directory_list , file_list



def get_result( command ):
    """ Send a command to the os shell and get the result as a string. """
    p = subprocess.Popen( command , stdout = subprocess.PIPE, stderr=subprocess.STDOUT )
    result_text, err_text = p.communicate()
    if err_text:
        return err_text
    else:
        return result_text


def convert( files , target_directory ):
    # COMMAND = 'ffmpeg -i filename.mp4 -f mp3 -ab 192000 -vn converted-filename.mp3
    for filename in files:
        if ':' in filename:
            print "FOUND"
            fixed_filename = filename.replace( ':', '-' )
            os.rename( filename, fixed_filename )
            print 'renamed: {} to {}'.format( filename, fixed_filename )
        if not os.path.exists( target_directory + filename + '.mp3' ):
            if '"' in filename:
                print 'escape double quote'
                os.system( 'avconv -i \'' + filename + '\' -f mp3 -ab 192000 -vn \'' + target_directory + filename + '.mp3\'' )
            else:
                os.system( 'avconv -i "' + filename + '" -f mp3 -ab 192000 -vn "' + target_directory + filename + '.mp3"' )

try:
  while True:
    directories, files = listing_as_directories_and_files( os.getcwd(), '.py' )
    convert( files, '/home/ubuntu/Desktop/music/converted/')
    time.sleep( 300 )

except KeyboardInterrupt:
  pass

print '\ndone'

  • « cli php q function print html line
  • encrypt decrypt »

Published

Nov 12, 2013

Category

python

~179 words

Tags

  • audio 1
  • avconv 2
  • command 29
  • convert 7
  • python 180