Skip to content

Commit

Permalink
Add mixpod command
Browse files Browse the repository at this point in the history
  • Loading branch information
peynaj committed Jun 11, 2021
1 parent d1fa8e1 commit 9dc61fa
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
13 changes: 11 additions & 2 deletions commands/mconv.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import os
import sys
from .utils import printable_report, change_audio_rate, video_to_audio_and_change_rate

from .utils import printable_report, change_audio_rate, video_to_audio_and_change_rate, mix_podcast_files

report = 'report'
convert = 'convert'
mixpod = 'mixpod'


def mconv_help():
hlp = f'''mconv
=====
mconv {report} [/path/to/directory (default:cwd)]
mconv {convert} [to=v/a (video/audio) (default=a)] [rate=2] [/path/to/directory (default:cwd)]
mconv {mixpod} [/path/to/directory (default:cwd)] [prefix of podcast directories default:2.]
====='''
print(hlp)
return
Expand Down Expand Up @@ -65,6 +66,14 @@ def main():
elif to_ == 'v':
video_to_audio_and_change_rate(path, rate)

elif command == mixpod:
path = os.getcwd()
prefix = '2.'
if len(sys.argv) > 2:
path = sys.argv[2]
if len(sys.argv) > 3:
prefix = sys.argv[3]
mix_podcast_files(path, prefix)
else:
print(f'Invalid command: {command}')
mconv_help()
72 changes: 72 additions & 0 deletions commands/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,78 @@ def printable_report(path):
return result


def mix_podcast_files(src_path, prefix):
if not os.path.exists(src_path):
print(f"Path not found: {src_path}")
return
all_directories = [path for path in os.listdir(src_path) if os.path.isdir(os.path.join(src_path, path))]
all_podcast_directories = [_dir for _dir in all_directories if _dir.startswith(prefix)]
all_podcast_directories.sort()
podcast_dir_to_files = dict()
print("* Podcast Directories:")
min_dir_file_count = 10**10
for _dir in all_podcast_directories:
path = os.path.join(src_path, _dir)
files = os.listdir(path)
files.sort()
podcast_dir_to_files[_dir] = files
count = len(files)
min_dir_file_count = min(count, min_dir_file_count)
print(f"* {count:02d} > {_dir}")
if count == 0:
do_rm_dir = input(f"### Remove empty directory: [{path}] [Y/n]? ")
if do_rm_dir.lower() == "y":
os.system(f"rm -rf {path}")

# print(podcast_dir_to_files)
# return

if min_dir_file_count == 0:
print(f"Minimum of directories file count is 0.\nFinished!")
return
loop_count = input(f"Enter loop count:\n[Default is {min_dir_file_count}. Enter blank to continue with {min_dir_file_count}]\n")
if loop_count and not loop_count.isalnum():
print("Invalid number!")
return
if not loop_count:
loop_count = min_dir_file_count
else:
loop_count = min(min_dir_file_count, int(loop_count))

# === Move files ===
dest_path = os.path.join(src_path, "0.2.pod.mix")
if not os.path.exists(dest_path):
os.system(f"mkdir {dest_path}")
print(f"* Dest path: {dest_path}")
last_index = 0
for file in os.listdir(dest_path):
file_name_parts = file.split(".")
index = file_name_parts[0]
if index.isalnum():
last_index = max(last_index, int(index))
last_index = last_index % 100
print(f"* Last Index: {last_index}")
for _ in range(loop_count):
for _dir in all_podcast_directories:
files = podcast_dir_to_files[_dir]
if not files:
print(f"Directory is empty: {_dir}\nFinished!")
return
last_index += 1
file = files[0]
podcast_dir_to_files[_dir] = files[1:]
file_name_parts = file.split(".")
file_name_base = ".".join(file_name_parts[:-1])
file_name_extension = file_name_parts[-1]
file_src_path = os.path.join(src_path, _dir, file)
file_dest_path = os.path.join(dest_path, f"{last_index:02d}.{file_name_base}.{_dir[len(prefix):]}.{file_name_extension}")
command = f"mv {file_src_path} {file_dest_path}"
print(f"> {command}")
os.system(command)
print("Finished!")
return


COLORS = dict(
HEADER='\033[95m',
OKBLUE='\033[94m',
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.2',
version='1.0.3',
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 9dc61fa

Please sign in to comment.