diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index ad69b73..0afc48c 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -162,8 +162,9 @@ "bases:Create new base": false } }, - "active": "50dd7c7842ea0193", + "active": "bd0227a0bc51fda3", "lastOpenFiles": [ + "metadata-stuff.md", "rename-nested-file-to-parent.md", "remove-timestamps.md", "remove-n-chars-from-name.md", diff --git a/auto-fill-missing-artist-tags.md b/auto-fill-missing-artist-tags.md deleted file mode 100644 index 862e702..0000000 --- a/auto-fill-missing-artist-tags.md +++ /dev/null @@ -1,50 +0,0 @@ -# Auto fill missing Artist/Title Tags - -```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 -``` diff --git a/bulk-move.md b/bulk-move.md index 11c181b..d4f09ae 100644 --- a/bulk-move.md +++ b/bulk-move.md @@ -12,3 +12,95 @@ 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." +``` diff --git a/filename-as-metadata-title.md b/filename-as-metadata-title.md deleted file mode 100644 index b196185..0000000 --- a/filename-as-metadata-title.md +++ /dev/null @@ -1,43 +0,0 @@ -# Filename as metadata title - -> 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" -``` - - ---- - - -```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." -``` diff --git a/metadata-stuff.md b/metadata-stuff.md new file mode 100644 index 0000000..b565a6d --- /dev/null +++ b/metadata-stuff.md @@ -0,0 +1,174 @@ +# 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 + +```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 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." +``` + + +--- + + +> 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." +``` diff --git a/regex-stuff.md b/regex-stuff.md index d248983..304f92e 100644 --- a/regex-stuff.md +++ b/regex-stuff.md @@ -87,3 +87,51 @@ for f in *; do 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." +```