Skip to content

Commit

Permalink
Add support for converting to .wav format
Browse files Browse the repository at this point in the history
  • Loading branch information
zackees committed Aug 2, 2024
1 parent f491950 commit d7fd6c5
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/zcmds/cmds/common/vid2mp3.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@ def run(
normalize: bool,
start: str | None,
end: str | None,
wave: bool,
) -> int:
if not os.path.exists(filename):
print(f"{filename} does not exist")
sys.exit(1)
if output:
out_path = output
assert out_path.endswith(".mp3")
assert out_path.endswith(".mp3") or (wave and out_path.endswith(".wav"))
else:
out_path = str(Path(filename).with_suffix(".mp3"))
out_path = str(Path(filename).with_suffix(".wav" if wave else ".mp3"))
cmd = f'static_ffmpeg -hide_banner -i "{filename}"'
if start:
cmd += f" -ss {start}"
if end:
cmd += f" -to {end}"
cmd += f' -vn -c:a libmp3lame -y ".{out_path}"'
if wave:
cmd += f' -vn -acodec pcm_s16le -y ".{out_path}"'
else:
cmd += f' -vn -c:a libmp3lame -y ".{out_path}"'
print(f"Executing:\n {cmd}")
os.system(cmd)
assert os.path.exists(f".{out_path}")
Expand All @@ -46,15 +50,18 @@ def run(

def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("filename", help="The video file to convert to mp3")
parser.add_argument("filename", help="The video file to convert to audio")
parser.add_argument("-o", "--output", help="The output file name")
parser.add_argument("-n", "--normalize", action="store_true")
parser.add_argument("--start", help="Start time for cutting (format: HH:MM:SS)")
parser.add_argument("--end", help="End time for cutting (format: HH:MM:SS)")
parser.add_argument(
"--wave", action="store_true", help="Generate .wav file instead of .mp3"
)
args = parser.parse_args()
filename = sys.argv[1:2][0]
print(f"Converting {filename} to mp3")
rtn = run(filename, args.output, args.normalize, args.start, args.end)
print(f"Converting {filename} to {'wav' if args.wave else 'mp3'}")
rtn = run(filename, args.output, args.normalize, args.start, args.end, args.wave)
return rtn


Expand Down

0 comments on commit d7fd6c5

Please sign in to comment.