Skip to content

Commit

Permalink
feat: add '--adjust-bitrate' option
Browse files Browse the repository at this point in the history
  • Loading branch information
lightpohl committed Aug 11, 2021
1 parent ef1ea48 commit 029ef66
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
43 changes: 22 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,28 @@

## Options

| Option | Type | Required | Description |
| ----------------------- | -------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| --url | String | true | URL to podcast RSS feed. |
| --out-dir | String | false | Specify output directory for episodes and metadata. Defaults to "./{{podcast_title}}". See "Templating" for more details. |
| --archive | String | false | Download or write out items not listed in archive file. Generates archive file at path if not found. See "Templating" for more details. |
| --episode-template | String | false | Template for generating episode related filenames. See "Templating" for details. |
| --include-meta | | false | Write out podcast metadata to JSON. |
| --include-episode-meta | | false | Write out individual episode metadata to JSON. |
| --ignore-episode-images | | false | Ignore downloading found images from --include-episode-meta. |
| --offset | Number | false | Offset starting download position. Default is 0. |
| --limit | Number | false | Max number of episodes to download. Downloads all by default. |
| --list-format | String (table\|json) | false | How to structure list data when logged. Default is "table". |
| --episode-regex | String | false | Match episode title against provided regex before starting download. |
| --add-mp3-metadata | | false | Attempts to add a base level of MP3 metadata to each episode. Recommended only in cases where the original metadata is of poor quality. (**ffmpeg required**) |
| --override | | false | Override local files on collision. |
| --reverse | | false | Reverse download direction and start at last RSS item. |
| --info | | false | Print retrieved podcast info instead of downloading. |
| --list | | false | Print episode list instead of downloading. |
| --exec | String | false | Execute a command after each episode is downloaded. |
| --version | | false | Output the version number. |
| --help | | false | Output usage information. |
| Option | Type | Required | Description |
| ----------------------- | -------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| --url | String | true | URL to podcast RSS feed. |
| --out-dir | String | false | Specify output directory for episodes and metadata. Defaults to "./{{podcast_title}}". See "Templating" for more details. |
| --archive | String | false | Download or write out items not listed in archive file. Generates archive file at path if not found. See "Templating" for more details. |
| --episode-template | String | false | Template for generating episode related filenames. See "Templating" for details. |
| --include-meta | | false | Write out podcast metadata to JSON. |
| --include-episode-meta | | false | Write out individual episode metadata to JSON. |
| --ignore-episode-images | | false | Ignore downloading found images from --include-episode-meta. |
| --offset | Number | false | Offset starting download position. Default is 0. |
| --limit | Number | false | Max number of episodes to download. Downloads all by default. |
| --list-format | String ("table" \| "json") | false | How to structure list data when logged. Default is "table". |
| --episode-regex | String | false | Match episode title against provided regex before starting download. |
| --add-mp3-metadata | | false | Attempts to add a base level of MP3 metadata to each episode. Recommended only in cases where the original metadata is of poor quality. (**ffmpeg required**) |
| --adjust-bitrate | String (e.g. "48k") | false | Attempts to adjust bitrate of MP3s. (**ffmpeg required**) |
| --override | | false | Override local files on collision. |
| --reverse | | false | Reverse download direction and start at last RSS item. |
| --info | | false | Print retrieved podcast info instead of downloading. |
| --list | | false | Print episode list instead of downloading. |
| --exec | String | false | Execute a command after each episode is downloaded. |
| --version | | false | Output the version number. |
| --help | | false | Output usage information. |

## Archive

Expand Down
14 changes: 14 additions & 0 deletions bin/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
writeFeedMeta,
writeItemMeta,
addMp3Metadata,
adjustBitrate,
runExec,
ITEM_LIST_FORMATS,
} = require("./util");
Expand Down Expand Up @@ -80,6 +81,10 @@ commander
"--add-mp3-metadata",
"attempts to add a base level of metadata to .mp3 files using ffmpeg"
)
.option(
"--adjust-bitrate <string>",
"attempts to adjust bitrate of .mp3 files using ffmpeg"
)
.option("--override", "override local files on collision")
.option("--reverse", "download episodes in reverse order")
.option("--info", "print retrieved podcast info instead of downloading")
Expand Down Expand Up @@ -126,6 +131,7 @@ const {
listFormat,
exec,
addMp3Metadata: addMp3MetadataFlag,
adjustBitrate: bitrate,
} = commander;

let { archive } = commander;
Expand Down Expand Up @@ -311,6 +317,14 @@ const main = async () => {
logItemInfo(item, LOG_LEVELS.important);
},
onAfterDownload: () => {
if (bitrate) {
try {
adjustBitrate({ outputPath: outputPodcastPath, bitrate });
} catch (error) {
logError("Unable to adjust bitrate", error);
}
}

if (addMp3MetadataFlag) {
try {
addMp3Metadata({
Expand Down
20 changes: 20 additions & 0 deletions bin/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,25 @@ const getFeed = async (url) => {
return feed;
};

const adjustBitrate = ({ outputPath, bitrate }) => {
if (!fs.existsSync(outputPath)) {
return;
}

if (!outputPath.endsWith(".mp3")) {
logError("Not an .mp3 file. Unable to adjust bitrate.");
return;
}

const tmpMp3Path = `${outputPath}.tmp.mp3`;
execSync(
`ffmpeg -loglevel quiet -i "${outputPath}" -b:a ${bitrate} "${tmpMp3Path}"`
);

fs.unlinkSync(outputPath);
fs.renameSync(tmpMp3Path, outputPath);
};

const addMp3Metadata = ({ feed, item, itemIndex, outputPath }) => {
if (!fs.existsSync(outputPath)) {
return;
Expand Down Expand Up @@ -509,6 +528,7 @@ module.exports = {
ITEM_LIST_FORMATS,
logItemsList,
addMp3Metadata,
adjustBitrate,
writeFeedMeta,
writeItemMeta,
runExec,
Expand Down

0 comments on commit 029ef66

Please sign in to comment.