Skip to content

Commit

Permalink
fix #417 switch to ruff (#440)
Browse files Browse the repository at this point in the history
* fix #417 switch to ruff

* first format

* apply W rule

* apply F rule

* apply C4 rule

* apply UP rule

* format ipynb
  • Loading branch information
lucemia authored Feb 11, 2025
1 parent 3ca5348 commit 2ff2d7d
Show file tree
Hide file tree
Showing 54 changed files with 3,041 additions and 982 deletions.
33 changes: 8 additions & 25 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,16 @@ repos:
- id: pretty-format-json
args: [--autofix]
exclude: "__snapshots__/"
# this is not technically always safe but usually is
# use comments `# isort: off` and `# isort: on` to disable/re-enable isort
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
args: [--line-length=120, --profile=black]

# this is slightly dangerous because python imports have side effects
# and this tool removes unused imports, which may be providing
# necessary side effects for the code to run
- repo: https://github.com/PyCQA/autoflake
rev: v1.6.1
hooks:
- id: autoflake
args:
- "--in-place"
- "--expand-star-imports"
- "--remove-duplicate-keys"
- "--remove-unused-variables"
- "--remove-all-unused-imports"

- repo: https://github.com/psf/black
rev: 22.8.0
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.9.1
hooks:
- id: black
args: [--line-length=120, --exclude=""]
# Run the linter.
- id: ruff
args: [--fix]
# Run the formatter.
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
Expand Down
12 changes: 3 additions & 9 deletions README.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,7 @@
"import ffmpeg\n",
"\n",
"# Flip video horizontally and output\n",
"f = (\n",
" ffmpeg\n",
" .input(filename='input.mp4')\n",
" .hflip()\n",
" .output(filename='output.mp4')\n",
")\n",
"f = ffmpeg.input(filename=\"input.mp4\").hflip().output(filename=\"output.mp4\")\n",
"f"
]
},
Expand Down Expand Up @@ -312,16 +307,15 @@
}
],
"source": [
"import ffmpeg.filters\n",
"import ffmpeg\n",
"import ffmpeg.filters\n",
"\n",
"# Complex filter graph example\n",
"in_file = ffmpeg.input(\"input.mp4\")\n",
"overlay_file = ffmpeg.input(\"overlay.png\")\n",
"\n",
"f = (\n",
" ffmpeg.filters\n",
" .concat(\n",
" ffmpeg.filters.concat(\n",
" in_file.trim(start_frame=10, end_frame=20),\n",
" in_file.trim(start_frame=30, end_frame=40),\n",
" )\n",
Expand Down
24 changes: 11 additions & 13 deletions docs/usage/basic-api-usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@
}
],
"source": [
"\n",
"import ffmpeg\n",
"\n",
"# Create a new input stream\n",
"ffmpeg.input('input.mp4')\n"
"ffmpeg.input(\"input.mp4\")"
]
},
{
Expand Down Expand Up @@ -108,7 +107,7 @@
"import ffmpeg\n",
"\n",
"# Create a new input stream with specific options\n",
"ffmpeg.input('input.mp4', ss=10, t=20) # Start at 10 seconds and last for 20 seconds\n"
"ffmpeg.input(\"input.mp4\", ss=10, t=20) # Start at 10 seconds and last for 20 seconds"
]
},
{
Expand Down Expand Up @@ -247,7 +246,9 @@
"import ffmpeg\n",
"\n",
"# Create and configure a new output stream\n",
"ffmpeg.input(\"input.mp4\").output(filename=\"output.mp4\", ss=10, t=20) # Output starting at 10 seconds with a duration of 20 seconds"
"ffmpeg.input(\"input.mp4\").output(\n",
" filename=\"output.mp4\", ss=10, t=20\n",
") # Output starting at 10 seconds with a duration of 20 seconds"
]
},
{
Expand Down Expand Up @@ -325,8 +326,8 @@
"import ffmpeg\n",
"\n",
"# Define input streams\n",
"input1 = ffmpeg.input('input1.mp4')\n",
"input2 = ffmpeg.input('input2.mp3')\n",
"input1 = ffmpeg.input(\"input1.mp4\")\n",
"input2 = ffmpeg.input(\"input2.mp3\")\n",
"\n",
"# Map multiple inputs to a single output\n",
"ffmpeg.output(input1, input2, filename=\"output.mp4\")"
Expand Down Expand Up @@ -407,10 +408,7 @@
"import ffmpeg\n",
"\n",
"# Chain input and output operations\n",
"(\n",
" ffmpeg.input('input1.mp4')\n",
" .output(ffmpeg.input(\"input2.mp3\"), filename=\"output.mp4\")\n",
")\n"
"(ffmpeg.input(\"input1.mp4\").output(ffmpeg.input(\"input2.mp3\"), filename=\"output.mp4\"))"
]
},
{
Expand Down Expand Up @@ -516,15 +514,15 @@
"import ffmpeg\n",
"\n",
"# Define input streams\n",
"input1 = ffmpeg.input('input1.mp4')\n",
"input2 = ffmpeg.input('input2.mp4')\n",
"input1 = ffmpeg.input(\"input1.mp4\")\n",
"input2 = ffmpeg.input(\"input2.mp4\")\n",
"\n",
"# Define output streams\n",
"output1 = input1.output(filename=\"output1.mp4\")\n",
"output2 = input2.output(filename=\"output2.mp4\")\n",
"\n",
"# Merge the outputs into a single operation\n",
"ffmpeg.merge_outputs(output1, output2)\n"
"ffmpeg.merge_outputs(output1, output2)"
]
}
],
Expand Down
62 changes: 34 additions & 28 deletions docs/usage/complex-filtering.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,12 @@
"import ffmpeg\n",
"\n",
"# Initialize the input stream from a video file\n",
"input_stream = ffmpeg.input('input.mp4')\n",
"input_stream = ffmpeg.input(\"input.mp4\")\n",
"\n",
"# Apply trimming and drawtext filters, then output to a new file\n",
"(\n",
" input_stream\n",
" .trim(start=10, end=20)\n",
" .drawtext(text='Hello World', fontsize=12, x=10, y=10)\n",
" input_stream.trim(start=10, end=20)\n",
" .drawtext(text=\"Hello World\", fontsize=12, x=10, y=10)\n",
" .output(filename=\"output.mp4\")\n",
")"
]
Expand Down Expand Up @@ -153,13 +152,19 @@
"import ffmpeg\n",
"\n",
"# Initialize an input stream from a video file\n",
"input1 = ffmpeg.input('input1.mp4') # The input stream here is an AVStream, capable of handling both audio and video operations.\n",
"input1 = ffmpeg.input(\n",
" \"input1.mp4\"\n",
") # The input stream here is an AVStream, capable of handling both audio and video operations.\n",
"\n",
"# Apply a reverse filter, which is specifically a video filter, to the stream\n",
"video = input1.reverse() # The 'reverse' filter is applied, resulting in a VideoStream output.\n",
"video = (\n",
" input1.reverse()\n",
") # The 'reverse' filter is applied, resulting in a VideoStream output.\n",
"\n",
"# Attempting to apply an audio filter (amix) to a video stream results in an error\n",
"ffmpeg.filters.amix(video) # This line will raise an exception because 'amix' is an audio filter, incompatible with a VideoStream."
"ffmpeg.filters.amix(\n",
" video\n",
") # This line will raise an exception because 'amix' is an audio filter, incompatible with a VideoStream."
]
},
{
Expand Down Expand Up @@ -256,15 +261,11 @@
"import ffmpeg\n",
"\n",
"# Initialize two input streams\n",
"input1 = ffmpeg.input('input1.mp4')\n",
"input2 = ffmpeg.input('input2.mp4')\n",
"input1 = ffmpeg.input(\"input1.mp4\")\n",
"input2 = ffmpeg.input(\"input2.mp4\")\n",
"\n",
"# Overlay one stream over the other and output to a new file\n",
"(\n",
" input1\n",
" .overlay(input2, x=10, y=10)\n",
" .output(filename=\"output.mp4\")\n",
")"
"(input1.overlay(input2, x=10, y=10).output(filename=\"output.mp4\"))"
]
},
{
Expand Down Expand Up @@ -422,15 +423,19 @@
"import ffmpeg\n",
"\n",
"# Initialize input streams\n",
"input1 = ffmpeg.input('input1.mp4')\n",
"input2 = ffmpeg.input('input2.mp4')\n",
"input1 = ffmpeg.input(\"input1.mp4\")\n",
"input2 = ffmpeg.input(\"input2.mp4\")\n",
"\n",
"# Apply a filter that generates multiple outputs\n",
"video, feedout = input1.feedback(input2) # Generates two output streams\n",
"\n",
"# Process and output each stream separately\n",
"o1 = video.drawtext(text='Hello World', fontsize=12, x=10, y=10).output(filename=\"output1.mp4\")\n",
"o2 = feedout.drawtext(text='Hello World', fontsize=12, x=10, y=10).output(filename=\"output2.mp4\")\n",
"o1 = video.drawtext(text=\"Hello World\", fontsize=12, x=10, y=10).output(\n",
" filename=\"output1.mp4\"\n",
")\n",
"o2 = feedout.drawtext(text=\"Hello World\", fontsize=12, x=10, y=10).output(\n",
" filename=\"output2.mp4\"\n",
")\n",
"\n",
"# Merge the outputs\n",
"ffmpeg.merge_outputs(o1, o2)"
Expand Down Expand Up @@ -515,8 +520,8 @@
"import ffmpeg\n",
"\n",
"# Correct usage with the expected number of inputs\n",
"input1 = ffmpeg.input('input1.mp4')\n",
"input2 = ffmpeg.input('input2.mp4')\n",
"input1 = ffmpeg.input(\"input1.mp4\")\n",
"input2 = ffmpeg.input(\"input2.mp4\")\n",
"ffmpeg.filters.amix(input1, input2, inputs=2)"
]
},
Expand Down Expand Up @@ -545,8 +550,8 @@
"import ffmpeg\n",
"\n",
"# Incorrect usage leading to a ValueError\n",
"input1 = ffmpeg.input('input1.mp4')\n",
"input2 = ffmpeg.input('input2.mp4')\n",
"input1 = ffmpeg.input(\"input1.mp4\")\n",
"input2 = ffmpeg.input(\"input2.mp4\")\n",
"f = ffmpeg.filters.amix(input1, input2, inputs=3) # This will raise a ValueError"
]
},
Expand Down Expand Up @@ -624,11 +629,12 @@
"source": [
"import ffmpeg\n",
"\n",
"input1 = ffmpeg.input('input1.mp4')\n",
"input2 = ffmpeg.input('input2.mp4')\n",
"input1 = ffmpeg.input(\"input1.mp4\")\n",
"input2 = ffmpeg.input(\"input2.mp4\")\n",
"\n",
"ffmpeg.filters.amix(input1, input2) # typed-ffmpeg will auto detect the correct inputs value\n",
"\n"
"ffmpeg.filters.amix(\n",
" input1, input2\n",
") # typed-ffmpeg will auto detect the correct inputs value"
]
},
{
Expand Down Expand Up @@ -658,15 +664,15 @@
"import ffmpeg\n",
"\n",
"# Initialize input and apply a split filter\n",
"input1 = ffmpeg.input('input1.mp4')\n",
"input1 = ffmpeg.input(\"input1.mp4\")\n",
"split = input1.split(outputs=2)\n",
"\n",
"# Correctly accessing the outputs\n",
"video0 = split.video(0)\n",
"video1 = split.video(1)\n",
"\n",
"# Incorrectly accessing an out-of-range output will raise an error\n",
"video2 = split.video(2) # This will raise a ValueError\n"
"video2 = split.video(2) # This will raise a ValueError"
]
}
],
Expand Down
15 changes: 9 additions & 6 deletions docs/usage/customizing-filters.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@
"# Apply a custom single-input video filter\n",
"(\n",
" ffmpeg.input(\"input.mp4\")\n",
" .vfilter(name=\"custom_filter\", option1=\"value1\", option2=\"value2\") # Apply \"custom_filter\" with specified options\n",
" .vfilter(\n",
" name=\"custom_filter\", option1=\"value1\", option2=\"value2\"\n",
" ) # Apply \"custom_filter\" with specified options\n",
" .output(filename=\"output.mp4\")\n",
")"
]
Expand Down Expand Up @@ -114,7 +116,9 @@
"source": [
"(\n",
" ffmpeg.input(\"input.mp4\")\n",
" .vfilter(name=\"custom_filter\", option1=\"value1\", option2=\"value2\") # Apply \"custom_filter\" with specified options\n",
" .vfilter(\n",
" name=\"custom_filter\", option1=\"value1\", option2=\"value2\"\n",
" ) # Apply \"custom_filter\" with specified options\n",
" .output(filename=\"output.mp4\")\n",
").compile_line()"
]
Expand Down Expand Up @@ -185,7 +189,7 @@
" ffmpeg.input(\"input.mp4\"),\n",
" name=\"custom_video_filter\",\n",
" option1=\"value1\",\n",
" option2=\"value2\"\n",
" option2=\"value2\",\n",
")"
]
},
Expand Down Expand Up @@ -268,8 +272,7 @@
"\n",
"# Create a multi-input custom video filter\n",
"(\n",
" ffmpeg.input(\"input1.mp4\")\n",
" .vfilter(\n",
" ffmpeg.input(\"input1.mp4\").vfilter(\n",
" ffmpeg.input(\"input2.mp4\"),\n",
" name=\"custom_video_filter\",\n",
" input_typings=(StreamType.video, StreamType.video),\n",
Expand Down Expand Up @@ -431,7 +434,7 @@
" name=\"custom_split\",\n",
" input_typings=(StreamType.video,),\n",
" output_typings=(StreamType.video, StreamType.video),\n",
")\n"
")"
]
},
{
Expand Down
Loading

0 comments on commit 2ff2d7d

Please sign in to comment.