Repository: m3at/video-watermark-removal Branch: main Commit: b62cf673e986 Files: 6 Total size: 5.2 KB Directory structure: gitextract_q4svttha/ ├── .gitignore ├── LICENSE ├── README.md ├── get_watermark.py ├── remove_watermark.sh └── test.sh ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # Local test files test_samples/ ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2021 Paul Willot 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. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ Remove static watermarks from videos with minimal setup. ![example of watermark removal](example_processed_frame.png) Really basic, but works well enough for simple static watermarks, and can run on a laptop CPU (x3 real-time on a i5-5287U (2015 MacBook Pro), x9 real-time on a i5-8400). You can find brief explanations on how it's done [here](https://paulw.tokyo/post/basic-watermark-removal-in-videos/). Dependencies: ```sh # FFMPEG installer=$([[ $(uname) == "Darwin" ]] && echo brew || echo apt) $installer install ffmpeg # Python libraries python3 -m pip install numpy scipy imageio # Optional, to fetch an example video # if already installed, make sure youtube-dl is up to date $installer install youtube-dl ``` Usage: ```sh # The output will default to append "_cleaned" to the existing name, # and use max 50 keyframes ./remove_watermark.sh /somewhere/my_video.mp4 [/somewhere/output.mp4] [max_keyframes_to_extract] ``` Tested on MacOS 10.14 (x86), MacOS 14.4 (arm) and Ubuntu 20.04 ================================================ FILE: get_watermark.py ================================================ #!/usr/bin/env python3 import sys from pathlib import Path import imageio import numpy as np from scipy.ndimage import gaussian_filter def normalize(x): _min = np.min(x) _max = np.max(x) return (x - _min) / (_max - _min) if __name__ == "__main__": # Load all images root = Path(sys.argv[1]) buff = [] for p in root.glob("output_*.png"): buff.append(imageio.imread(p)) images = np.array(buff) # Compute the gradients dx = np.gradient(images, axis=1).mean(axis=3) dy = np.gradient(images, axis=2).mean(axis=3) mean_dx = np.abs(np.mean(dx, axis=0)) mean_dy = np.abs(np.mean(dy, axis=0)) # Filter at a hand picked threshold threshold = 10 salient = ((mean_dx > threshold) | (mean_dy > threshold)).astype(float) salient = normalize(gaussian_filter(salient, sigma=3)) mask = ((salient > 0.2) * 255).astype(np.uint8) # Saved the computed mask imageio.imsave(root / "mask.png", mask) ================================================ FILE: remove_watermark.sh ================================================ #!/usr/bin/env bash set -eo pipefail # Prepare output name file_no_ext="${1%.*}" extension="${1##*.}" def_name="$file_no_ext""_cleaned.""$extension" output_file="${2:-$def_name}" # Get first few key frames echo "Getting key frames..." max_frames="${3:-50}" keyframes_time=$(ffprobe -hide_banner -loglevel warning -select_streams v -skip_frame nokey -show_frames -show_entries frame=pkt_dts_time "$1" | grep "pkt_dts_time=" | xargs shuf -n "$max_frames" -e | awk -F "=" '{print $2}') # Save them as images, in a temporary directory tmpdir=$(mktemp -d 2>/dev/null || mktemp -d -t 'watermark_remove') counter=0 echo -n "Extracting frames (up to: $max_frames)... " for i in $keyframes_time; do if ! [[ "$i" =~ ^[0-9]+([.][0-9]+)?$ ]]; then echo "Skipping unrecognize timing: $i" continue fi ffmpeg -y -hide_banner -loglevel error -ss "$i" -i "$1" -vframes 1 "$tmpdir/output_$counter.png" echo -n "$counter " ((counter=counter+1)) done echo # Abort if we couldn't extract frames for some reason if [[ "$counter" -lt 2 ]]; then echo "$counter frames extracted, need at least 2, aborting." exit 1 fi echo "Extracting watermark..." ./get_watermark.py "$tmpdir" echo "Removing watermark in video..." ffmpeg -hide_banner -loglevel warning -y -stats -i "$1" -acodec copy -vf "removelogo=$tmpdir/mask.png" "$output_file" rm -rf "$tmpdir" echo "Done" exit 0 ================================================ FILE: test.sh ================================================ #!/usr/bin/env bash set -eo pipefail # Store samples there mkdir -p test_samples # Get the first 3 minutes of Spring (Blender Open Movie) after the opening credits, 720p video only echo "Fetching sample movie" URL=$(youtube-dl -g -f136 "https://youtu.be/WhWc3b3KhnY") ffmpeg -hide_banner -loglevel warning -y -stats -ss 00:23 -t 03:00 -i "$URL" -c copy ./test_samples/original.mp4 # Add simple text as watermark echo "Adding watermark" ffmpeg -hide_banner -loglevel warning -y -stats -i ./test_samples/original.mp4 -filter_complex "[0:v]drawtext='font=sans-serif:fontsize=30:fontcolor=white:x=20:y=20:text=Watermark (TM)'" ./test_samples/watermarked.mp4 # Test watermark removal echo "Tesing:" echo ./remove_watermark.sh test_samples/watermarked.mp4