add a bunch of cool new stuff

This commit is contained in:
2026-07-09 01:05:56 +02:00
parent 9c9bcab59f
commit 2fa6ca798f
130 changed files with 5645 additions and 501 deletions

View File

@@ -0,0 +1,21 @@
# Prefix
> Add prefix to files
```bash
for f in *; do
if [ -f "$f" ]; then
mv "$f" "PREFIX - $f"
fi
done
```
---
> Add prefix to dirs
```bash
for d in */ ; do
mv "$d" "PREFIX - $d"
done
```

View File

@@ -0,0 +1,11 @@
# Bring files up one dir
> Bring files up by one level from their nested dirs
```bash
find . -mindepth 2 -type f \( -name "*.mkv" -o -name "*.mp4" -o -name "*.avi" \) -exec sh -c '
for file do
mv -- "$file" "$(dirname "$file")/.."
done
' sh {} +
```

106
misc/bulk-move.md Normal file
View File

@@ -0,0 +1,106 @@
# Bulk Move
```bash
#!/bin/bash
INPUT_FILE="folders_and_files_to_move.txt"
DESTINATION_DIR="/path/to/dir"
while read -r dir; do
mv "$dir" "$DESTINATION_DIR"
done < "$INPUT_FILE"
echo "All specified directories have been moved to '$DESTINATION_DIR'"
```
---
> Find folders with prefixes from a file, move into target, nested in folder called as the prefix currently being processed, case insensitive
```bash
#!/bin/bash
SOURCE_DIR="/path/to/source/dir"
TARGET_DIR="/path/to/target/dir"
LIST_FILE="move_with_prefix.txt"
while IFS= read -r line || [[ -n "$line" ]]; do
echo "----------------------"
echo "Processing line: $line"
TARGET_SUBDIR="$TARGET_DIR/$line"
if [ ! -d "$TARGET_SUBDIR" ]; then
mkdir -p "$TARGET_SUBDIR"
fi
find "$SOURCE_DIR" -maxdepth 1 -type d -iname "$line - *" | while read -r src_dir; do
echo "Moving '$src_dir' to '$TARGET_SUBDIR'"
mv "$src_dir" "$TARGET_SUBDIR/"
done
done < "$LIST_FILE"
echo "Directories have been processed and moved."
```
---
> Find all folders that have either string in their name, case insensitive
```bash
#!/bin/bash
SOURCE_DIR="/path/to/dir"
STRINGS_FILE="find.txt"
OUTPUT_FILE="matches.txt"
> "$OUTPUT_FILE"
while IFS= read -r line || [[ -n "$line" ]]; do
echo "-------------------------------"
echo "Processing line $line"
echo "String: $line" >> "$OUTPUT_FILE"
find "$SOURCE_DIR" -type d -iname "*$line*" -exec echo " - {}" \; >> "$OUTPUT_FILE"
done < "$STRINGS_FILE"
echo "Search is complete. Results are saved in $OUTPUT_FILE."
```
---
> Find folders containing particular strings from a file (one by one), move into target, nested in folder called as the string currently being processed, case insensitive
```bash
#!/bin/bash
# Set the path to the file with strings, the source directory, and the target directory
SOURCE_DIR="/path/to/source/dir"
TARGET_DIR="/path/to/target/dir"
STRINGS_FILE="find.txt"
while IFS= read -r line || [[ -n "$line" ]]; do
echo "----------------------"
echo "Processing line $line"
# Create the target subdirectory if it doesn't exist
TARGET_SUBDIR="$TARGET_DIR/$line"
if [ ! -d "$TARGET_SUBDIR" ]; then
mkdir -p "$TARGET_SUBDIR"
fi
# Search for and move directories containing the string, case-insensitive, depth of 1
find "$SOURCE_DIR" -maxdepth 1 -type d -iname "*$line*" | while read -r src_dir; do
echo "Moving '$src_dir' to '$TARGET_SUBDIR'"
mv "$src_dir" "$TARGET_SUBDIR/"
done
done < "$STRINGS_FILE"
echo "Move operation is complete."
```

112
misc/ffmpeg.md Normal file
View File

@@ -0,0 +1,112 @@
# Ffmpeg
> Convert mkv to gif
```bash
ffmpeg -i "input.mkv" -vf "fps=10,scale=320:-1:flags=lanczos" -c:v gif -f gif "output.gif"
```
```bash
ffmpeg -i "input.mkv" -vf "fps=30,scale=1920:-1:flags=lanczos" -c:v gif -f gif "output.gif"
```
---
> Convert mkv to gif and get rid of that pattern
```bash
ffmpeg -i "input.mkv" -vf palettegen palette.png
ffmpeg -i "input.mkv" -i palette.png -lavfi "fps=30,scale=1920:-1:flags=lanczos [x]; [x][1:v] paletteuse=dither=none" "output.gif"
```
---
> Convert mkv to mp4
```bash
ffmpeg -i "input.mkv" -codec copy "output.mp4"
```
---
> Convert flac to mp3
```bash
ffmpeg -i "input.flac" -ab 320k "output.mp3"
```
---
> Detect silence
```bash
ffmpeg -i "Song.flac" -af silencedetect=noise=-30dB:d=5 -f null - 2>&1 | grep 'silence_'
```
---
> Check tags
```bash
ffprobe -v error -show_entries format_tags -of default=noprint_wrappers=1 "Song.flac"
```
---
> Extract aac from avi
```bash
ffmpeg -i input-video.avi -vn -acodec copy output-audio.aac
```
---
> Increase volume of mp3
```bash
ffmpeg -i input.mp3 -af volume=1.5 output.mp3
```
---
> Convert DTS to AAC
```bash
ffmpeg -i "Movie [DTS] [1080p].mp4" -c:v copy -c:a aac -b:a 256k "Movie [AAC] [1080p].mp4"
```
---
> Check for errors
```bash
ffprobe -v error -show_entries frame=pkt_pts_time -of csv=p=0 "input.mp4" > errors.log
ffmpeg -v error -i "output.mp4" -c copy -f null - 2> errors2.log
```
---
> Get audio channels + languages of a file
```bash
ffprobe -v error -select_streams a -show_entries stream=index,codec_type,channels,channel_layout,codec_name,codec_long_name:stream_tags=language -of default=noprint_wrappers=1 "input.mkv"
```
---
> Cut up to certain timestamp
```bash
ffmpeg -i "input.mkv" -to 01:37:49 -c copy -map 0 "output.mkv"
```
---
> Cut starting from specific timeframe
```bash
ffmpeg -i "input.mp4" -ss 00:05:28 -c copy -avoid_negative_ts 1 "output.mp4"
```
---
> Extract single frame within timeframe close to keyframe
```bash
ffmpeg -ss 00:02:07 -to 00:02:10 -i "input.mp4" -vf "select='eq(pict_type,PICT_TYPE_I)'" -vsync vfr thumbnail_%03d.png
```
---
> Extract single frame at specific timestamp
```bash
ffmpeg -ss 00:00:03 -i "input.mp4" -vf "select='eq(pict_type,PICT_TYPE_I)'" -frames:v 1 output.png
```
---

59
misc/find-short-videos.md Normal file
View File

@@ -0,0 +1,59 @@
# Find short Videos
```bash
#!/bin/bash
DURATION_THRESHOLD_SECONDS=180
IGNORED_FOLDERS=("backdrops")
OUTPUT_FILE="short_video_dirs.txt"
> "$OUTPUT_FILE"
find . -type f -name "*.mp4" | while read -r file; do
parent_dir=$(dirname "$file")
for ignored_folder in "${IGNORED_FOLDERS[@]}"; do
if [[ "$parent_dir" == *"$ignored_folder"* ]]; then
continue 2
fi
done
length=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file")
length=$(printf "%.0f\n" "$length")
if [ "$length" -lt "$DURATION_THRESHOLD_SECONDS" ]; then
grep -qxF "$parent_dir" "$OUTPUT_FILE" || echo "$parent_dir" >> "$OUTPUT_FILE"
fi
done
echo "Operation complete. The list of parent directories has been saved to $OUTPUT_FILE."
```
```bash
#!/bin/bash
DURATION_THRESHOLD_SECONDS=180
IGNORED_FOLDERS=("backdrops")
OUTPUT_FILE="short_video_dirs.txt"
> "$OUTPUT_FILE"
find . -type f -name "*.mp4" | while read -r file; do
parent_dir=$(dirname "$file")
for ignored_folder in "${IGNORED_FOLDERS[@]}"; do
if [[ "$parent_dir" == *"$ignored_folder"* ]]; then
continue 2
fi
done
length=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file")
length=$(printf "%.0f\n" "$length")
if [ "$length" -lt "$DURATION_THRESHOLD_SECONDS" ]; then
echo "$file" >> "$OUTPUT_FILE"
fi
done
echo "Operation complete. The list of parent directories has been saved to $OUTPUT_FILE."
```

242
misc/metadata-stuff.md Normal file
View File

@@ -0,0 +1,242 @@
# Metadata Stuff
> Search the current directory for MP4 files with the specified artist's name and append the paths to the output file.
```bash
#!/bin/bash
ARTIST_NAME="Artist Name"
OUTPUT_FILE="artist_mp4_paths.txt"
> "$OUTPUT_FILE"
find . -type f -iname "*.mp4" -exec bash -c 'ffmpeg -i "$1" 2>&1 | grep -q "$0" && echo "$1"' "$ARTIST_NAME" '{}' \; >> "$OUTPUT_FILE"
echo "All paths have been stored in $OUTPUT_FILE"
```
---
> Auto fill missing Artist/Title Tags based on existing tags
> This throws some weird errors where it says "mnt/path/to/dir.flac not found" (note the missing / at the beginning of the path). Happens with both absolute and relative. Doesn't throw for every file. Rerun the script until no more errors and files missing to process
```bash
#!/bin/bash
DIRECTORY="/path/to/dir"
LOGFILE="/path/to/dir/logfile.log"
artist_like_attributes=( "ARTISTS" "ARTIST SORT ORDER" "ALBUM ARTIST" "ALBUM ARTIST SORT ORDER" )
title_like_attributes=( "ALBUM" )
find "$DIRECTORY" -type f \( -iname "*.flac" -o -iname "*.mp3" -o -iname "*.opus" \) | while IFS= read -r file; do
echo "Processing file: $file" >> $LOGFILE
artist=$(ffprobe -v error -show_entries format_tags=artist -of default=noprint_wrappers=1:nokey=1 "$file")
title=$(ffprobe -v error -show_entries format_tags=title -of default=noprint_wrappers=1:nokey=1 "$file")
if [ -n "$artist" ]; then
metadata_to_set=()
for attribute in "${artist_like_attributes[@]}"; do
value=$(ffprobe -v error -show_entries "format_tags=$attribute" -of default=noprint_wrappers=1:nokey=1 "$file")
if [ -z "$value" ]; then
metadata_to_set+=(-metadata "$attribute=$artist")
fi
done
for attribute in "${title_like_attributes[@]}"; do
value=$(ffprobe -v error -show_entries "format_tags=$attribute" -of default=noprint_wrappers=1:nokey=1 "$file")
if [ -z "$value" ]; then
metadata_to_set+=(-metadata "$attribute=$title")
fi
done
if [ ${#metadata_to_set[@]} -ne 0 ]; then
temp_file=$(dirname "$file")/temp_$(basename "$file")
ffmpeg -i "$file" -c copy "${metadata_to_set[@]}" -y "$temp_file" && mv -f "$temp_file" "$file"
if [ $? -eq 0 ]; then
echo "Successfully updated: $file" >> $LOGFILE
else
echo "FFmpeg failed for: $file" >> $LOGFILE
fi
fi
fi
done
```
---
> Set the title in the metadata of all mp4 files based on the respective filename
```bash
for FILE_PATH in *.mp4; do
if [ ! -f "$FILE_PATH" ]; then
continue
fi
BASE_NAME=$(basename "$FILE_PATH" .mp4)
TEMP_FILE="${FILE_PATH}_temp.mp4"
ffmpeg -i "$FILE_PATH" -c copy -metadata title="$BASE_NAME" "$TEMP_FILE" && mv "$TEMP_FILE" "$FILE_PATH"
done
echo "Metadata title set for all MP4 files in directory"
```
---
> Set artist recursively
```bash
ARTIST="Artist Name"
for DIR in */ ; do
if [ -d "$DIR" ]; then
for FILE_PATH in "$DIR"*.mp4; do
if [ ! -f "$FILE_PATH" ]; then
continue
fi
BASE_NAME=$(basename "$FILE_PATH" .mp4)
TEMP_FILE="${FILE_PATH}_temp.mp4"
ffmpeg -i "$FILE_PATH" -c copy -metadata artist="$ARTIST" "$TEMP_FILE" && mv "$TEMP_FILE" "$FILE_PATH"
done
fi
done
echo "Metadata artist set for all MP4 files in directory"
```
---
> Set the title in the metadata of all mp4 files based on the respective filename, recursively
```bash
#!/bin/bash
for DIR in */ ; do
if [ -d "$DIR" ]; then
for FILE_PATH in "$DIR"*.mp4; do
if [ ! -f "$FILE_PATH" ]; then
continue
fi
BASE_NAME=$(basename "$FILE_PATH" .mp4)
TEMP_FILE="${FILE_PATH%.*}_temp.mp4"
ffmpeg -i "$FILE_PATH" -c copy -metadata title="$BASE_NAME" "$TEMP_FILE" && mv -f "$TEMP_FILE" "$FILE_PATH"
done
fi
done
echo "Metadata title set for all MP4 files in subdirectories."
```
```bash
#!/bin/bash
for DIR in */ ; do
if [ -d "$DIR" ]; then
for FILE_PATH in "$DIR"*.mp4; do
if [ ! -f "$FILE_PATH" ]; then
continue
fi
# Get base name without extension
BASE_NAME=$(basename "$FILE_PATH" .mp4)
TEMP_FILE="${FILE_PATH%.*}_temp.mp4"
# Fetch current comment metadata
CURRENT_COMMENT=$(ffprobe -v error -show_entries format_tags=comment -of default=noprint_wrappers=1:nokey=1 "$FILE_PATH")
CURRENT_TITLE=$(ffprobe -v error -show_entries format_tags=title -of default=noprint_wrappers=1:nokey=1 "$FILE_PATH")
echo "Current Comment: $CURRENT_COMMENT"
echo "Current Title: $CURRENT_TITLE"
# If there's no current comment, set it to the old base name
# Otherwise, append the old base name to the current comment
if [ -z "$CURRENT_COMMENT" ]; then
NEW_COMMENT=$BASE_NAME
else
NEW_COMMENT="${CURRENT_COMMENT}; Old Title: ${CURRENT_TITLE}"
fi
# Use ffmpeg to set title and update the comment
ffmpeg -i "$FILE_PATH" -c copy -metadata title="$BASE_NAME" -metadata comment="$NEW_COMMENT" "$TEMP_FILE" && mv -f "$TEMP_FILE" "$FILE_PATH"
done
fi
done
echo "Metadata title and comment updated for all MP4 files in subdirectories."
```
---
> Prefix files with artist, if any
```bash
#!/bin/bash
TARGET_DIR="."
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
# Function to extract artist name using ffprobe (part of ffmpeg suite)
extract_artist() {
ffprobe -v error -show_entries format_tags=artist -of default=noprint_wrappers=1:nokey=1 "$1"
}
# Function to rename the file and its parent directory
rename_with_artist() {
local file_path="$1"
local artist_name="$2"
local parent_dir
local filename
# Extract the parent directory and the filename from the file path
parent_dir=$(dirname "$file_path")
filename=$(basename "$file_path")
# Replace any forward slashes in artist name to avoid directory traversal
artist_name=$(echo "$artist_name" | sed 's/\//_/g')
if [[ ! -z "$artist_name" ]]; then
mv "$file_path" "${parent_dir}/${artist_name} - ${filename}"
mv "$parent_dir" "${parent_dir%/*}/${artist_name} - ${parent_dir##*/}"
fi
}
find "$TARGET_DIR" -type f -iname "*.mp4" | while read -r file; do
echo "----------------------"
echo "Processing file: $file"
artist=$(extract_artist "$file")
if [[ ! -z "$artist" ]]; then
echo "Artist found: $artist - Renaming file and directory..."
rename_with_artist "$file" "$artist"
else
echo "No artist found for file: $file"
fi
done
IFS=$SAVEIFS
echo "All files and directories have been processed."
```

View File

@@ -0,0 +1,20 @@
# Move file into dir with same name
> Move all mp4 files into a dir with the same name as the file
```bash
#!/bin/bash
find . -maxdepth 1 -type f -iname "*.mp4" -exec bash -c '
for file in "$@"; do
# Extract the base name without the extension
base="${file%.mp4}"
# Create a new directory with the base name
mkdir -p "${base}"
# Move the MP4 file into the newly created directory
mv "$file" "${base}/"
done
' bash {} +
```

View File

@@ -0,0 +1,22 @@
# Move files to year and month dirs
> Extract date from folder name and move into year and month separated sub dirs
```bash
#!/bin/bash
for dir in */; do
if [[ -d "$dir" ]]; then
if [[ $dir =~ ([0-9]{4}-[0-9]{2}-[0-9]{2}) ]]; then
date=${BASH_REMATCH[1]}
year=${date:0:4}
month=${date:5:2}
mkdir -p "$year/$month"
mv "$dir" "$year/$month/"
else
echo "The directory '$dir' does not contain a valid date."
fi
fi
done
```

17
misc/open-urls-from-file.sh Executable file
View File

@@ -0,0 +1,17 @@
#!/bin/bash
# Path to the text file containing URLs
url_file="open_urls.txt"
# Check if the file with URLs exists
if [[ ! -f "$url_file" ]]; then
echo "File with URLs not found: $url_file"
exit 1
fi
# Iterate over lines in the file and open each URL
while IFS= read -r url; do
ungoogled-chromium --new-tab "$url"
done < "$url_file"
echo "All URLs have been opened in new tabs."

View File

@@ -0,0 +1,41 @@
# Print all files and folders
> Save all file and folder names in a directory to a file
```bash
#!/bin/bash
OUTPUT_FILE="directory_contents.txt"
> "$OUTPUT_FILE"
echo "Listing all files and folders in current dir"
for item in *; do
if [ -f "$item" ]; then
echo "$(basename "$item")" >> "$OUTPUT_FILE"
elif [ -d "$item" ]; then
echo "$(basename "$item")" >> "$OUTPUT_FILE"
fi
done
echo "Contents have been written to $OUTPUT_FILE."
```
---
> Get all file and folder names in a directory
```bash
#!/bin/bash
echo "Listing all files and folders in current dir"
for item in *; do
if [ -f "$item" ]; then
echo "$(basename "$item")"
elif [ -d "$item" ]; then
echo "$(basename "$item")"
fi
done
```

View File

@@ -0,0 +1,31 @@
# Recursively extract artist from filename
> Used to extract the first artist name, assuming it sits between the first " - " and second " - "
```bash
#!/bin/sh
SEARCH_DIR="/path/to/dir"
OUTPUT_FILE="artist_names.txt"
cd "$SEARCH_DIR" || exit
> "$OUTPUT_FILE"
find . -type f -name '* - *' | while read -r file; do
intermediate_string=$(echo "$file" | sed -n 's/.* - \(.*\) - .*/\1/p')
artist=$(echo "$intermediate_string" | \
sed -e 's/ x .*//i' \
-e 's/ X .*//i' \
-e 's/ ft\. .*//i' \
-e 's/ feat\. .*//i' \
-e 's/ & .*//i' \
-e 's/, .*//i' \
-e 's/ + .*//i')
echo "$artist" >> "$OUTPUT_FILE"
done
cd - || exit
```

151
misc/regex-stuff.md Normal file
View File

@@ -0,0 +1,151 @@
# Regex Stuff
> Remove everything from first `?` (including) in filenames recursively
```bash
#!/bin/bash
START_DIR="."
find "$START_DIR" -type f -print0 | while IFS= read -r -d '' file; do
dir=$(dirname "$file")
base=$(basename "$file")
new_base="${base%%\?*}"
if [[ "$base" != "$new_base" ]]; then
mv -n "$file" "${dir}/${new_base}"
fi
done
echo "File renaming complete."
```
---
> Extract from strings in file
```bash
grep -oP "href='\K[^']+?(?=')" "Bookmarks.html" > urls.txt
grep -oP "HREF=\"\K[^\"]+?(?=\")" "Bookmarks2.html" > urls2.txt
```
---
> Replace something in dir name
```bash
search_string="REPLACE ME - "
replace_string=""
for d in */ ; do
d="${d%/}"
if [[ "$d" == *"$search_string"* ]]; then
new_d="${d/$search_string/$replace_string}"
mv -- "$d" "$new_d"
fi
done
```
---
> Replace anywhere, case insensitive
```bash
for f in *; do
if [ -f "$f" ]; then
newname=$(echo "$f" | sed 's/some\.string/A new String/gi')
mv -- "$f" "$newname"
fi
done
```
---
> Replace something at beginning of filename
```bash
for f in *; do
if [ -f "$f" ]; then
newname=$(echo "$f" | sed 's/^some\.string/A new String/')
mv -- "$f" "$newname"
fi
done
```
---
> Replace anywhere in filename
```bash
for f in *; do
if [ -f "$f" ]; then
newname=$(echo "$f" | sed 's/BEFORE/After/gi')
mv -- "$f" "$newname"
fi
done
```
---
> Extract content between second last `_` and last `_` assuming theres some resolution classifier after the last underscore
```bash
#!/bin/bash
OUTPUT_FILE="extracted_content.txt"
> "$OUTPUT_FILE"
for dir in */; do
dir="${dir%/}"
if [[ $dir =~ _([^_]+)_(144|240p|360p|480p|720p|1080p|1440p|2160p)$ ]]; then
echo "${BASH_REMATCH[1]}" >> "$OUTPUT_FILE"
fi
done
echo "Extraction complete. Content saved to $OUTPUT_FILE."
```
---
> Extract content before first occurrence of ` - `
```bash
#!/bin/bash
OUTPUT_FILE="extracted_content.txt"
> "$OUTPUT_FILE"
for dir in */; do
dir="${dir%/}"
if [[ $dir =~ ^([^\-]+)\ - ]]; then
echo "${BASH_REMATCH[1]}" >> "$OUTPUT_FILE"
fi
done
echo "Extraction complete. Content saved to $OUTPUT_FILE."
```
---
> Replace first 4 characters
```bash
for f in * ; do
if [[ -f "$f" ]]; then
new_f="${f:4}"
mv -- "$f" "$new_f"
fi
done
```

View File

@@ -0,0 +1,51 @@
# Remove N chars from name
```bash
#!/bin/bash
TARGET_DIR="."
REMOVE_N_CHARS=8
for dir in "$TARGET_DIR/*/"; do
dir="${dir%/}"
new_dir="${dir:$REMOVE_N_CHARS}"
mv -- "$dir" "$new_dir"
done
```
---
```bash
#!/bin/bash
TARGET_DIR="."
REMOVE_N_CHARS=8
generate_unique_dir_name() {
local base_name=$1
local counter=2
local new_name
while true; do
new_name="${base_name}_${counter}"
if [[ ! -d "$new_name" ]]; then
echo "$new_name"
break
fi
((counter++))
done
}
for dir in "$TARGET_DIR/*/"; do
dir="${dir%/}"
new_dir="${dir:$REMOVE_N_CHARS}"
if [[ -d "$new_dir" ]]; then
new_dir=$(generate_unique_dir_name "$new_dir")
fi
mv -- "$dir" "$new_dir"
done
```

18
misc/remove-timestamps.md Normal file
View File

@@ -0,0 +1,18 @@
# Remove timestamps
> Remove timestamps from files and folders
```bash
#!/bin/bash
for file in [0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]*; do
if ! [[ $file =~ ^[0-9][0-9]-[0-9][0-9]-[0-9]{4}\ [0-9]{1,2}#[0-9]{2}\ (AM|PM)\ -\ ]]; then
continue
fi
newname=$(echo "$file" | sed -E 's/^[0-9]{2}-[0-9]{2}-[0-9]{4} [0-9]{1,2}#[0-9]{2} (AM|PM) - //')
mv -- "$file" "$newname"
done
echo "Files have been renamed."
```

View File

@@ -0,0 +1,56 @@
# Rename nested file to parent
> Go through all folders, check if mp4 is in there. If so, rename the mp4 to the same as it's respective parent folder
```bash
#!/bin/bash
for dir in */; do
if [[ -d "$dir" ]]; then
dir_name="${dir%/}"
mp4_file=$(find "$dir" -maxdepth 1 -type f -name '*.mp4' | head -n 1)
if [[ -n "$mp4_file" ]]; then
new_filename="${dir_name}.mp4"
mv -n "$mp4_file" "${dir}/${new_filename}"
echo "Renamed '$mp4_file' to '${dir}/${new_filename}'"
else
echo "No .mp4 file found in $dir"
fi
fi
done
```
---
> Rename parent folder to nested mp4
```bash
#!/bin/bash
for dir in */; do
if [[ -d "$dir" ]]; then
mp4_file=$(find "$dir" -maxdepth 1 -type f -name '*.mp4' | head -n 1)
if [[ -n "$mp4_file" ]]; then
mp4_base_name="$(basename "${mp4_file}" .mp4)"
trimmed_dir_name="${dir%/}"
new_dir_name="${mp4_base_name}"
if [[ "$trimmed_dir_name" != "$new_dir_name" ]]; then
if [[ ! -d "$new_dir_name" ]]; then
mv -n "$dir" "$new_dir_name"
echo "Renamed directory '$dir' to '$new_dir_name'"
else
echo "Cannot rename '$dir' to '$new_dir_name' because the target directory already exists."
fi
fi
else
echo "No .mp4 file found in $dir"
fi
fi
done
```

31
misc/yandex-extract-img-url.sh Executable file
View File

@@ -0,0 +1,31 @@
#!/bin/sh
# Validate input
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <input_file_with_urls> <output_file_for_extracted_img_urls>"
exit 1
fi
input_file="$1"
output_file="$2"
error_file="error_urls.txt"
# Loop through each URL, extract, decode, and save the 'img_url', with indexing
index=0
while IFS= read -r line; do
# Increment our index counter
index=$((index+1))
# Extract the 'img_url' query parameter
extracted_url=$(echo "$line" | awk -F 'img_url=' '{print $2}' | cut -d'&' -f1 | python3 -c "import sys; from urllib.parse import unquote; print(unquote(sys.stdin.read()));")
# Save and print the results
if [ -n "$extracted_url" ]; then
echo "$index: $extracted_url"
echo "$extracted_url" >> "$output_file"
else
echo "$index: No 'img_url' found"
echo "$line" >> "$error_file"
fi
done < "$input_file"