Skip to content

Commit

Permalink
fixing config oversights (#37)
Browse files Browse the repository at this point in the history
* fixing config oversights

* remove config as a param

* README adjustments.

* more json readme adjustments
  • Loading branch information
zfleeman authored Dec 17, 2024
1 parent 504ec45 commit 4b6bdd4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ ff4d cool_clip.mp4 --from 00:00:10 --to 00:00:30 -s 10
I've had a good time using this command with a Batch file on Windows. Refer to the [Sample Batch File](#sample-batch-file) section for more information.

### Optional Flags

| Flag | Default | Example | Description |
|---|---|---|---|
| `-o`<br>`--output` | current working directory | `-o "C:/Users/zflee/A Folder"`<br>`-o "C:/Users/zflee/Desktop/A Folder/filename.mp4"` | If you want your smaller clips to go to a specific folder, use this option. You can also choose a custom output filename, just make sure to include the correct file extension for your video codec. |
Expand All @@ -69,6 +70,7 @@ I've had a good time using this command with a Batch file on Windows. Refer to t
| `--vp9-opts` | No default. | `--vp9-opts '{"row-mt":1,"deadline":"good","cpu-used":2}'` | Specify options to tweak VP9 encoding speed. `row-mt`, `deadline`, and `cpu-used` are the only values supported at the moment. This can only be set with the command line or JSON configuration file. It is not configurable with the Web UI. |

### File Name Formatting

Enable this feature with `--filename-times`. You can edit the name of your video file if you need to trim it to a specific section. Here are a few examples.

1) `000020.mp4`
Expand All @@ -79,24 +81,31 @@ Enable this feature with `--filename-times`. You can edit the name of your video
- Compresses the entire video if the first six characters of the file's name aren't numeric.

### JSON Configuration
If your encoding job will always be the same, you can reference a JSON configuration file instead of passing a long list of arguments to the command line. If you use the configuration JSON and supply a duplicate flag, the flag will be used over the JSON value. For example, if `"target_filesize"` is a key in your JSON _and_ `--target-filesize` (or `-s`) has been supplied in the command line, the value you give the command line will be used.

```
If your encoding job uses the same settings consistently, you can simplify your workflow by referencing a JSON configuration file instead of specifying multiple command-line arguments. This feature was designed for advanced users or "workflow people."

When using both a JSON configuration file and command-line flags, the **command-line flag values take precedence** over the values defined in the JSON. For example, if `"target_filesize"` is specified in the JSON file and you include the `--target-filesize` (or `-s`) flag in your command, the command-line value will be used.

If you supply a configuration JSON file, include only the settings that differ from the [default values](#optional-flags). Any omitted values will automatically use their defaults.

#### Example JSON File Configuration

Create a new plain text JSON file (`my-config.json`) structured like this:

```json
{
"target_filesize": 10.0,
"resolution": "1280x720",
"codec": "libx264",
"from": "00:00:00",
"codec": "libvpx-vp9",
"to": "00:00:40"
}
```

Notes:
- All of the keys except for `"from"` and `"to"` must always be present. Those entries can be deleted if you do not have a timestamp entry for the given field. Examples:
- `"times": {}` -> if you do not wish to trim the start and stop time of the file.
- `"times": {"from": "00:00:10"}` -> trim the clip from `00:00:10` to the end of the file
- `"times": {"to": "00:00:20"}` -> trim the clip from the beginning of the file up to `00:00:20`
- You can set `audio_br` to `null` if you want to maintain the clip's audio bitrate.
And then you would call `ff4d` like this:

```bash
ff4d my-video.mp4 --config my-config.json
```

## Detailed Example

Expand Down
11 changes: 6 additions & 5 deletions ffmpeg4discord/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ def get_args() -> Namespace:
if args["config"]:
file_path = Path(args["config"]).resolve()
with open(file_path) as f:
config = json.load(f)
config: dict = json.load(f)

for k, v in config.items():
if not args[k]:
args[k] = v
elif args[k] == parser.get_default(k):
args[k] = v

del args["config"]

# do some work regarding the port
if args["web"]:
Expand Down Expand Up @@ -107,13 +111,10 @@ def get_args() -> Namespace:
del args["to"]

# work with vp9 options
if args["vp9_opts"]:
if args["vp9_opts"] and not isinstance(args["vp9_opts"], dict):
logging.info(f"Received VP9 options: {args['vp9_opts']}")
try:
args["vp9_opts"] = json.loads(args["vp9_opts"])
if not isinstance(args["vp9_opts"], dict):
logging.error("The `vp9-opts` input must be a dictionary. Using default parameters.")
args["vp9_opts"] = None
except json.JSONDecodeError:
logging.error(
"""Invalid JSON format. Format your input string like this: \'{"row-mt": 1, "deadline": "good", "cpu-used": 2}\'. Using default parameters."""
Expand Down
1 change: 0 additions & 1 deletion ffmpeg4discord/twopass.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def __init__(
codec: str = "libx264",
crop: str = "",
resolution: str = "",
config: Optional[str] = None,
filename_times: bool = False,
framerate: Optional[int] = None,
vp9_opts: Optional[dict] = None,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "ffmpeg4discord"
version = "0.1.6"
version = "0.1.7"
description = "A tool to help convert a video file to a target file size."
readme = {file = "README.md", content-type = "text/markdown"}
authors = [{ name = "Zach Fleeman", email = "zfleeman@gmail.com" }]
Expand Down

0 comments on commit 4b6bdd4

Please sign in to comment.