-
Notifications
You must be signed in to change notification settings - Fork 2k
Add filename extension for music files with no extension during import #6457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+234
−1
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c3967ca
Sync fork with master and paste in changes
gaotue e812f6a
add missing test files
gaotue ace43c8
fix windows tests
gaotue 5b0fb0e
fix windows tests
gaotue 31d4a12
add config & doc, refactor, optimize
gaotue 2285671
refactoring and other improvements
gaotue 652733a
format
gaotue 2ba11da
clarify changelog
gaotue f9874d5
Merge branch 'master' into master
gaotue e8ff69b
fix doc format
gaotue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| # This file is part of beets. | ||
| # Copyright 2016, Adrian Sampson. | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person obtaining | ||
| # a copy of this software and associated documentation files (the | ||
| # "Software"), to deal in the Software without restriction, including | ||
| # without limitation the rights to use, copy, modify, merge, publish, | ||
| # distribute, sublicense, and/or sell copies of the Software, and to | ||
| # permit persons to whom the Software is furnished to do so, subject to | ||
| # the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be | ||
| # included in all copies or substantial portions of the Software. | ||
|
|
||
| """A tool that finds an extension for files without one""" | ||
|
|
||
| import os | ||
| import subprocess | ||
| from logging import Logger | ||
| from pathlib import Path | ||
|
|
||
| import beets | ||
| from beets import util | ||
|
|
||
| logger = Logger.info | ||
|
|
||
| PathBytes = bytes | ||
| PATH_SEP: bytes = util.bytestring_path(os.sep) | ||
|
|
||
| # a list of audio formats I got from wikipedia https://en.wikipedia.org/wiki/Audio_file_format | ||
| AUDIO_EXTENSIONS = { | ||
| "3gp", | ||
| "aa", | ||
| "aac", | ||
| "aax", | ||
| "act", | ||
| "aiff", | ||
| "alac", | ||
| "amr", | ||
| "ape", | ||
| "au", | ||
| "awb", | ||
| "dss", | ||
| "dvf", | ||
| "flac", | ||
| "gsm", | ||
| "iklax", | ||
| "ivs", | ||
| "m4a", | ||
| "m4b", | ||
| "m4p", | ||
| "mmf", | ||
| "movpkg", | ||
| "mp1", | ||
| "mp2", | ||
| "mp3", | ||
| "mpc", | ||
| "msv", | ||
| "nmf", | ||
| "ogg", | ||
| "oga", | ||
| "mogg", | ||
| "opus", | ||
| "ra", | ||
| "rm", | ||
| "raw", | ||
| "rf64", | ||
| "sln", | ||
| "tta", | ||
| "voc", | ||
| "vox", | ||
| "wav", | ||
| "wma", | ||
| "wv", | ||
| "webm", | ||
| "8svx", | ||
| "cda", | ||
| } | ||
|
|
||
|
|
||
| def fix_extension(path_bytes: PathBytes, logger: Logger | None = None): | ||
| """Return the `path` after adding an appropriate extension if needed. | ||
|
|
||
| If the file already has an extension, return as-is. | ||
| If the file has no extension, try to find the format using ffprobe. | ||
| If the file is not a music format, return as-is. | ||
| If the format is found, return path with extension. | ||
| """ | ||
| path = Path(os.fsdecode(path_bytes)) | ||
| # if there is an extension, return unchanged | ||
| if path.suffix != "": | ||
| return path_bytes | ||
|
|
||
| # no extension detected | ||
| # use ffprobe to find the format | ||
| formats = [] | ||
| shell = os.name == "nt" | ||
| if ( | ||
| subprocess.run( | ||
| ["ffprobe", "-version"], capture_output=True, shell=shell | ||
| ).stderr.decode("utf-8") | ||
| != "" | ||
| ): | ||
| if logger: | ||
| logger.error("ffprobe needed to determine file extension") | ||
| output = subprocess.run( | ||
| [ | ||
| "ffprobe", | ||
| "-hide_banner", | ||
| "-loglevel", | ||
| "fatal", | ||
| "-show_format", | ||
| "--", | ||
| str(path), | ||
| ], | ||
| capture_output=True, | ||
| shell=shell, | ||
| ) | ||
| out = output.stdout.decode("utf-8") | ||
| err = output.stderr.decode("utf-8") | ||
| if err != "": | ||
| if logger: | ||
| logger.error("Error with ffprobe\n", err) | ||
| for line in out.split("\n"): | ||
| if line.startswith("format_name="): | ||
| formats = line.split("=")[1].split(",") | ||
| detected_format = "" | ||
| # The first format from ffprobe that is on this list is taken | ||
| for f in formats: | ||
| if f in AUDIO_EXTENSIONS: | ||
| detected_format = f | ||
| break | ||
|
|
||
| # if ffprobe can't find a format, the file is prob not music | ||
| if detected_format == "": | ||
| return path_bytes | ||
|
|
||
| # cp and add ext. If already exist, use that file | ||
| # assume, for example, the only diff between 'asdf.mp3' and 'asdf' is format | ||
| new_path = path.with_suffix("." + detected_format) | ||
| if not new_path.exists(): | ||
| if beets.config["import"]["fix_ext_inplace"]: | ||
| util.move(bytes(path), bytes(new_path)) | ||
| else: | ||
| util.copy(bytes(path), bytes(new_path)) | ||
| else: | ||
| if logger: | ||
| logger.info("Import file with matching format to original target") | ||
| return new_path |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.