Skip to content

Commit

Permalink
Improve conv for subdirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
peynaj committed Sep 27, 2023
1 parent 243f7c6 commit 85b9607
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
55 changes: 29 additions & 26 deletions commands/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,36 @@


def change_audio_rate(dir_path, speed_rate=2):
prefix = '{}'.format(speed_rate)
if os.path.exists(dir_path):
folder = dir_path.split('/')[-1]
new_dir_path = '/'.join(dir_path.split('/')[:-1]) + '/' + prefix + '.' + folder
os.mkdir(new_dir_path)
l = list(os.listdir(dir_path))
l.sort()
for f in l: # f -> file
fpath = dir_path + '/' + f
fname = '.'.join(f.split('.')[:-1])
new_fpath = new_dir_path + '/' + fname + '.' + prefix + '.mp3'
'''
if not os.path.exists(new_fpath):
open(new_fpath, 'a').close()
'''
for c in conv:
fpath = fpath.replace(c, f"\{c}")
new_fpath = new_fpath.replace(c, f"\{c}")
prefix = str(speed_rate)
if not os.path.exists(dir_path):
print(f'directory not exists: {dir_path}')
return
dir_items = list(os.listdir(dir_path))
dir_items.sort()
if all(map(lambda item: os.path.isdir(os.path.join(dir_path, item)), dir_items)):
for directory in dir_items:
change_audio_rate(os.path.join(dir_path, directory), speed_rate)
return

comm = 'ffmpeg -i {old_file} -filter:a "atempo={rate}" -vn {new_file} -y'.format(
old_file=fpath,
rate=speed_rate,
new_file=new_fpath,
)
print('\n=======================\n', comm, '\n==========================\n')
os.system(comm)
print('# {} -> finished!'.format(new_fpath))
folder = dir_path.split('/')[-1]
new_dir_path = '/'.join(dir_path.split('/')[:-1]) + '/' + prefix + '.' + folder
os.mkdir(new_dir_path)
for file in dir_items:
fpath = dir_path + '/' + file
fname = '.'.join(file.split('.')[:-1])
new_fpath = new_dir_path + '/' + fname + '.' + prefix + '.mp3'
for c in conv:
fpath = fpath.replace(c, f"\{c}")
new_fpath = new_fpath.replace(c, f"\{c}")

comm = 'ffmpeg -i {old_file} -filter:a "atempo={rate}" -vn {new_file} -y'.format(
old_file=fpath,
rate=speed_rate,
new_file=new_fpath,
)
print('\n=======================\n', comm, '\n==========================\n')
os.system(comm)
print('# {} -> finished!'.format(new_fpath))


def video_to_audio_and_change_rate(dir_path, speed_rate=2):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='mconv',
version='1.0.4',
version='1.0.5',
description='A convert system for playing speed of audio and video files by ffmpeg',
long_description=README,
author='Peyman Najafi',
Expand Down

0 comments on commit 85b9607

Please sign in to comment.