Skip to content

Commit

Permalink
Merge branch '39.support-local-input-files' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
gth828r committed Sep 26, 2024
2 parents e4e6c45 + c7bfa81 commit 05cf926
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
samples/christmas.mp3 binary
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*.mp3
!samples/christmas.mp3
*.mp4
*.egg-info
.coverage
.env
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,20 @@ The simplest way to get started with Brownify is to find a song you want to modi
```sh
# This example assumes that the brownify source is located in your home directory
cd ~/brownify
brownify https://www.youtube.com/watch?v=B4RqeAvE7iA boognish-christmas.mp3 --recipe-file recipes/boognish-brown
brownify --youtube-input https://www.youtube.com/watch?v=B4RqeAvE7iA boognish-christmas.mp3 --recipe-file recipes/boognish-brown
```

The result of this command is a file `boognish-christmas.mp3` located in the current directory.

Sometimes Youtube makes changes which break the downloader libraries that are available. If this happens and you still want to try brownify, you can also pass a path to a local file as input. Below is an example using a cached version of the Christmas song:
```sh
# This example assumes that the brownify source is located in your home directory
cd ~/brownify
brownify --local-input samples/christmas.mp3 boognish-christmas.mp3 --recipe-file recipes/boognish-brown
```

Note that these examples use the following song (the locally stored version just being a short sample), which was released with no copyright under the creative commons license:
It's Not Christmas Time (Without You) [feat. Kimmy Baggins] – RYYZN

## Python API
The python documentation for the latest release can be found [here.](https://brownify.readthedocs.io/en/latest/brownify.html)
40 changes: 32 additions & 8 deletions brownify/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import shutil
import uuid
from typing import Optional

from brownify.downloaders import YoutubeDownloader
from brownify.errors import BrownifyError, InvalidInputError
Expand All @@ -16,7 +17,14 @@

def get_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Make your music brown")
parser.add_argument("youtube", help="URL for a youtube video")

input_group = parser.add_mutually_exclusive_group(required=True)
input_group.add_argument(
"--youtube-input", help="URL for a youtube video to use as input"
)
input_group.add_argument(
"--local-input", help="Path to a local file to use as input"
)
parser.add_argument(
"output", help="Filename to send output to (will be an mp3)"
)
Expand Down Expand Up @@ -62,7 +70,7 @@ def _get_program(recipe, recipe_file) -> str:

def _cleanup(downloaded_file, session_id):
try:
# Clean the intermediate downloaded file
# Clean the intermediate downloaded file if it exists
os.remove(downloaded_file)
except OSError as ose:
logging.warning(f"Could not remove downloaded file {downloaded_file}")
Expand All @@ -83,7 +91,8 @@ def _cleanup(downloaded_file, session_id):
def main() -> int:
args = get_args()

youtube_url = args.youtube
youtube_url: Optional[str] = args.youtube_input
local_file: Optional[str] = args.local_input
output_file = args.output
log_level = args.log.upper()
preserve = args.preserve
Expand All @@ -101,8 +110,9 @@ def main() -> int:
# Create a random ID for tracking this session
session_id = str(uuid.uuid4())

# Create downloaded filename based on session ID
downloaded_file = f"{session_id}.mp4"
# Declare input filename to be set by either the local file
# or by downloading an audio file from youtube
input_file: Optional[str] = None

try:
# Get the program from user input
Expand All @@ -113,13 +123,27 @@ def main() -> int:
pipelines = ap.get_pipelines(program)

# Grab an audio file based on the provided inputs
YoutubeDownloader.get_audio(youtube_url, downloaded_file)
if youtube_url is not None:
# Create download filename based on session ID
download_file = f"{session_id}.mp4"
YoutubeDownloader.get_audio(youtube_url, download_file)
input_file = download_file
elif local_file is not None:
_, local_file_ext = os.path.splitext(local_file)
input_file = f"{session_id}{local_file_ext}"
# Create a symlink to the local file for this session
os.symlink(local_file, input_file)
else:
raise InvalidInputError(
"For audio input, a path to a local file or a youtube URL "
"must be provided"
)

# Split the input audio into different tracks
splitter = AudioSplitterFactory.get_audio_splitter(
AudioSplitterType.FIVE_STEM
)
splitter.split(downloaded_file)
splitter.split(input_file)

# Perform processing pipeline over the audio
processor = PipelineProcessor(session_id, splitter)
Expand All @@ -138,4 +162,4 @@ def main() -> int:
# Clean up temporary files unless they have been marked for
# preservation
if not preserve:
_cleanup(downloaded_file, session_id)
_cleanup(input_file, session_id)
Binary file added samples/christmas.mp3
Binary file not shown.
12 changes: 6 additions & 6 deletions tests/end-to-end/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ NC="\033[0m"
log_path=${test_abs_dir}/test.log
recipe_path=${test_abs_dir}/../../recipes/boognish-brown
# The following is a creative commons licensed song
url=https://www.youtube.com/watch?v=B4RqeAvE7iA
local_file=samples/christmas.mp3
workspace=${test_abs_dir}/workspace
set_c=
set_l=
Expand All @@ -24,7 +24,7 @@ USAGE="USAGE: $test_filename [-h] [-c|-l LOG] [-r RECIPE] [-u URL]
-c Log to console [Default: Disabled]
-l LOG Path to log file [Default: ${log_path}]
-r RECIPE Path to a recipe file [Default: ${recipe_path}]
-u URL Youtube URL for source song [Default: ${url}]
-i INPUT Path to input file for source song [Default: ${local_file}]
"

while getopts ":chl:o:r:u:" opt; do
Expand All @@ -44,8 +44,8 @@ while getopts ":chl:o:r:u:" opt; do
r )
recipe_path=$(realpath --no-symlinks $OPTARG)
;;
u )
url=$OPTARG
i )
local_file=$OPTARG
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
Expand Down Expand Up @@ -98,8 +98,8 @@ try() {
try log setup
try log pushd ${workspace}

# Run brownify
try log brownify ${url} ${workspace}/${OUTFILE} --recipe-file ${recipe_path}
# Run brownify using a local input file
try log brownify --local-input ${local_file} ${workspace}/${OUTFILE} --recipe-file ${recipe_path}

# See if a file was successfully created
if [ -f ${workspace}/${OUTFILE} ]; then
Expand Down

0 comments on commit 05cf926

Please sign in to comment.