Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Share your Debian SCRIPTS

Here you can discuss every aspect of Debian. Note: not for support requests!
Message
Author
User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Share your Debian SCRIPTS

#41 Post by Hallvor »

This script is used to retrieve simple information and movie ratings from the command line interface. I registered for an API-key for up to 1000 hits a day, so I am sharing that as well. You need to install the command line JSON-processor to make the script work.

Code: Select all

# apt install jq 
And here is the script:

Code: Select all

#!/bin/bash

# Prompt the user for the movie title
echo "Enter the movie title:"
read movie_title

# Set the API key
api_key="4664a2b1"

# Get the movie information
movie_info=$(curl -s "http://www.omdbapi.com/?apikey=$api_key&t=$movie_title")

# Extract the movie's title, year, and plot
title=$(echo "$movie_info" | jq -r ".Title")
year=$(echo "$movie_info" | jq -r ".Year")
plot=$(echo "$movie_info" | jq -r ".Plot")

# Print the movie information
echo "$title ($year)"
echo "$plot"

# Extract the movie's rating
rating=$(echo "$movie_info" | jq -r ".imdbRating")

# Print the movie's rating
echo "The IMDB-rating for $movie_title is $rating/10"

Example output:

Code: Select all

hallvor@debian-thinkpad:~$ ./get_movie_info.sh
Enter the movie title:
Platoon
Platoon (1986)
Chris Taylor, a neophyte recruit in Vietnam, finds himself caught in a battle of wills between two sergeants, one good and the other evil. A shrewd examination of the brutality of war and the duality of man in conflict.
The IMDB-rating for Platoon is 8.1/10
hallvor@debian-thinkpad:~$ 
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
sunrat
Administrator
Administrator
Posts: 6412
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 116 times
Been thanked: 461 times

Re: Share your Debian SCRIPTS

#42 Post by sunrat »

Hallvor wrote: 2023-01-08 13:05 This script is used to retrieve simple information and movie ratings from the command line interface.
Nice one! I look up IMDB ratings regularly so may actually use this. :D
It doesn't do disambiguation. I searched for "The White Lotus" and got the TV series but "White Lotus" returned some mysterious unrated result which doesn't even appear in a website search. Guess it is fine as long as the exact title is used and doesn't have multiple films with the same title.
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Share your Debian SCRIPTS

#43 Post by Hallvor »

Thanks for the input: How about something like this? The script will also ask for year of release, in case there are several movies with the same name. (You can leave it empty if you want.)

Code: Select all

#!/bin/bash

# Prompt the user for the movie title and year
echo "Enter the movie title:"
read movie_title
echo "Enter the movie's year of release:"
read movie_year

# Set the API key
api_key="4664a2b1"

# Get the movie information
movie_info=$(curl -s "http://www.omdbapi.com/?apikey=$api_key&t=$movie_title&y=$movie_year")

# Extract the movie's title, year, and plot
title=$(echo "$movie_info" | jq -r ".Title")
year=$(echo "$movie_info" | jq -r ".Year")
plot=$(echo "$movie_info" | jq -r ".Plot")
genre=$(echo "$movie_info" | jq -r ".Genre")
runtime=$(echo "$movie_info" | jq -r ".Runtime")

# Print the movie information
echo "$title ($year)"
echo "$plot"
echo "$genre"
echo "$runtime"

# Extract the movie's rating
rating=$(echo "$movie_info" | jq -r ".imdbRating")

# Print the movie's rating
echo "The IMDB-rating for $movie_title is $rating/10"
Example:

Code: Select all

hallvor@debian-thinkpad:~$ ./get_movie_info.sh
Enter the movie title:
All quiet on the western front
Enter the movie's year of release:
1930
All Quiet on the Western Front (1930)
A German youth eagerly enters World War I, but his enthusiasm wanes as he gets a firsthand view of the horror.
The IMDB-rating for All quiet on the western front is 8.1/10
hallvor@debian-thinkpad:~$ ./get_movie_info.sh
Enter the movie title:
All quiet on the western front 
Enter the movie's year of release:
2022
All Quiet on the Western Front (2022)
A young German soldier's terrifying experiences and distress on the western front during World War I.
The IMDB-rating for All quiet on the western front is 7.8/10
hallvor@debian-thinkpad:~$ 
Edit: added genre and runtime.

Example:

Code: Select all

hallvor@debian-thinkpad:~$ film
Enter the movie title:
platoon
Enter the movie's year of release:

Platoon (1986)
Chris Taylor, a neophyte recruit in Vietnam, finds himself caught in a battle of wills between two sergeants, one good and the other evil. A shrewd examination of the brutality of war and the duality of man in conflict.
Drama, War
120 min
The IMDB-rating for platoon is 8.1/10
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
sunrat
Administrator
Administrator
Posts: 6412
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 116 times
Been thanked: 461 times

Re: Share your Debian SCRIPTS

#44 Post by sunrat »

Hallvor wrote: 2023-01-08 14:36 Thanks for the input: How about something like this? The script will also ask for year of release, in case there are several movies with the same name. (You can leave it empty if you want.)
Good if you know the year. If you don't, you may have to visit IMDB website to find out by which time you have done the search there anyway. :lol:
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Share your Debian SCRIPTS

#45 Post by Hallvor »

Yes, IMDB would likely be faster if there are remakes or TV-series and you don't know the year. That probably means that the script will be faster most of the time. ;)
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Share your Debian SCRIPTS

#46 Post by Hallvor »

Here is a boxing round timer

Features:
* Set number of rounds, minutes per round and number of seconds between rounds
* Plays a sound at the start of each round, when ten seconds are remaining and when the round is over
* Shows time left in ASCII (with figlet)

Prerequisites:
* figlet: # apt install figlet
* mplayer (can be changed to whatever you like)
* bell sounds for start and finish of each round (not included)
* stick sound when 10 seconds remaining (also not included)

Code: Select all

#!/bin/bash

# Get user input for the number of rounds, minutes per round, and seconds between rounds
echo "Enter the number of rounds: "
read rounds
echo "Enter the number of minutes per round: "
read minutes
echo "Enter the number of seconds between rounds: "
read between_rounds

# Calculate the total number of seconds per round
total_seconds=$((minutes * 60))

# Run the round timer
for (( i=1; i<=rounds; i++ )); do
  # Play the start of round audio file
  mplayer start_of_round.mp3

  # Run the countdown timer for the current round
  while [ $total_seconds -gt 0 ]; do
    # Print the current time remaining as ASCII art
    printf "Round $i: "
    figlet -f standard -c "$((total_seconds/60)):$((total_seconds%60))"

    # Play a sound ten seconds before the end of the round
    if [ $total_seconds -eq 10 ]; then
      mplayer ten_seconds.mp3
    fi

    # Sleep for one second
    sleep 1

    # Decrement the total number of seconds by 1
    total_seconds=$((total_seconds - 1))
  done

  # Play the end of round audio file
  mplayer end_of_round.mp3

  # Reset the total number of seconds for the next round
  total_seconds=$((minutes * 60))

  # Sleep for the specified number of seconds between rounds
  sleep $between_rounds
done

# Print a message when the timer is finished
echo "Time's up!"
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
wizard10000
Global Moderator
Global Moderator
Posts: 557
Joined: 2019-04-16 23:15
Location: southeastern us
Has thanked: 75 times
Been thanked: 85 times

Re: Share your Debian SCRIPTS

#47 Post by wizard10000 »

I use this one to recursively normalize mp3 volume levels. This could easily be an alias but I documented it fairly well, which is why it's a script :)

disclaimer: I didn't write this but I did tweak it a tiny bit and commented the heck out of it. This little oneliner resampled and normalized > 20k mp3s in about two hours.

Code: Select all

#!/bin/bash

# xargs:  see 'man xargs'
# '-0' configures input so mp3gain can use it.
# '-P 4' is the number of parallel mp3gain processes to run. you can 
# play with this to find what works best for you but it would be wise to
# not exceed the number of physical + virtual cores in your machine.

# mp3gain:  see 'man mp3gain'
# '-r' this switch applies track gain as opposed to album gain.
# '-s r' tells mp3gain to ignore mp3 tags and resample the file to get volume levels.
# '-d 2.0' increases volume X number of decibels above the default 89db.  negative
# numbers are supported.  you may or may not want this - i do it because mp3 play
# a couple db lower than sirius xm in my car does.

find . -name "*.mp3" -print0 | xargs -0 -n 1 -P 4 mp3gain -r -s r -d 2.0
we see things not as they are, but as we are.
-- anais nin

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Share your Debian SCRIPTS

#48 Post by Hallvor »

Here is a script to automate upgrades and reboot the system automatically in case of a kernel upgrade. It should work well enough for lazy people running a home server. The script will also clean up downloaded package files by default, but that can easily be removed.

I added the script to crontab to run at 03.00 each night.

Code: Select all

# crontab -e

Code: Select all

0 3 * * * /path/to/script.sh
And here it is:

Code: Select all

#!/bin/bash

# Read the current kernel version
current_kernel=$(uname -r|cut -d '-' -f 1)

# Update package list
su -c "apt update"

# Upgrade installed packages
su -c "apt upgrade -y"

# Check if kernel has been upgraded
new_kernel=$(dpkg -l linux-image-*.*|awk '/^ii/{print $2}'|grep -v -e $(uname -r|cut -f1,2 -d"-")|grep -e [0-9]|cut -d'-' -f3)

if [[ $current_kernel != $new_kernel ]]; then
    echo "Kernel has been upgraded, rebooting..."
    su -c "reboot"
else
    echo "Kernel has not been upgraded"
fi

# Clean up downloaded package files
su -c "apt autoclean -y"
Last edited by Hallvor on 2023-01-10 17:52, edited 2 times in total.
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
Bloom
df -h | grep > 90TiB
df -h | grep > 90TiB
Posts: 504
Joined: 2017-11-11 12:23
Been thanked: 26 times

Re: Share your Debian SCRIPTS

#49 Post by Bloom »

The dpkg-queries yield a long list of kernels in my system, so I suggest the following changes:

Code: Select all

current_kernel=$(uname -r|cut -d '-' -f 1)
...
new_kernel=$(dpkg -l linux-image-*.*|awk '/^ii/{print $2}'|grep -v -e $(uname -r|cut -f1,2 -d"-")|grep -e [0-9]|cut -d'-' -f3)

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Share your Debian SCRIPTS

#50 Post by Hallvor »

Thanks, Bloom!

Tested and running fine. Updated.
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Share your Debian SCRIPTS

#51 Post by Hallvor »

And here is a lossless to mp3 batch audio converter:

Supported lossless audio files types: .flac,.alac and .wav.

Put all your lossless audio folders/files in a directory. Enter that directory as input directory. You will then be prompted for an output directory. If the output directory doesn't exist, it will be created automatically.

The script will copy all folders and respect the structure of the input directory.

Select mp3 quality level from 0 (worst) to 9 (best).

Start the script and pick up your finished mp3-files in the output directory.

Code: Select all

#!/bin/bash

read -p "Enter the input directory: " input_dir
read -p "Enter the output directory: " output_dir
read -p "Enter the desired quality (0-9): " quality

if [ ! -d "$output_dir" ]; then
  mkdir -p "$output_dir"
fi

for file in $(find "$input_dir" \( -name '*.flac' -o -name '*.alac' -o -name '*.wav' \) -type f); do
    relative_path="$(dirname "${file#$input_dir}")"
    output_path="$output_dir/$relative_path"
    mkdir -p "$output_path"
    ffmpeg -i "$file" -q:a $quality -map a "$output_path/$(basename "${file%.*}").mp3"
done
Last edited by Hallvor on 2023-01-11 09:27, edited 1 time in total.
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
sunrat
Administrator
Administrator
Posts: 6412
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 116 times
Been thanked: 461 times

Re: Share your Debian SCRIPTS

#52 Post by sunrat »

Hallvor wrote: 2023-01-11 08:00 And here is a lossless to mp3 batch audio converter:
Neat script but mp3 is an inferior quality legacy codec. Ogg Vorbis and now Opus are far superior and are also FOSS. The script could be easily adapted to use vorbisenc or opusenc.
I would only use mp3 for a player which absolutely supports only mp3.

A really neat converter script is acxi from h2, developer of inxi and smxi. https://github.com/smxi/acxi . It can recursively convert a whole collection from lossless to lossy.
acxi is a tool that syncs/converts lossless (flac, wav, raw) music libraries to
compressed (mp3,ogg,opus) versions of the lossless library. It also can convert
aif, raw, shn, and wav to flac. It also creates/checks md5, ffp files, tags your
collection (read man page), embeds images, and much more.
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Share your Debian SCRIPTS

#53 Post by Hallvor »

Aren't you a tough customer? :wink:

Added opus and ogg-support. This script can also recursively convert an entire collection.

Code: Select all

#!/bin/bash

read -p "Enter the input directory: " input_dir
read -p "Enter the output directory: " output_dir
read -p "Enter the desired quality (0-9): " quality
read -p "Enter the desired audio format (mp3, opus, ogg): " audio_format

if [ ! -d "$output_dir" ]; then
  mkdir -p "$output_dir"
fi

IFS=$'\n'
for file in $(find "$input_dir" \( -name '*.flac' -o -name '*.alac' -o -name '*.wav' \) -type f); do
    relative_path="$(dirname "${file#$input_dir}")"
    output_path="$output_dir/$relative_path"
    mkdir -p "$output_path"
    case $audio_format in
        "mp3") ffmpeg -i "$file" -q:a $quality -map a "$output_path/$(basename "${file%.*}").mp3";;
        "opus") ffmpeg -i "$file" -b:a 256k -vbr on "$output_path/$(basename "${file%.*}").opus";;
        "ogg") ffmpeg -i "$file" -q:a $quality -map a -c:a libvorbis "$output_path/$(basename "${file%.*}").ogg";;
    esac
done

Edit: I altered the script to handle songs with spaces in their names.
Edit2: I altered the default bitrate for opus to 256k.
Last edited by Hallvor on 2023-01-11 15:30, edited 1 time in total.
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
sunrat
Administrator
Administrator
Posts: 6412
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 116 times
Been thanked: 461 times

Re: Share your Debian SCRIPTS

#54 Post by sunrat »

Hallvor wrote: 2023-01-11 09:15 Aren't you a tough customer? :wink:
Can confirm. :mrgreen: I'm fussy about sound quality. I have some nice studio monitors and did some A/B tests with a friend who is also a sound engineer and has better ears than me. He could pick mp3 almost every time against flac, but not vorbis or opus.
Added opus and ogg-support. This script can also recursively convert an entire collection.
Excellent! I love how open source software can improve by a simple bug report or fix, suggestion, or feature request. I regularly like to pester help developers even though I'm no l33t coder or anything. :wink:
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Share your Debian SCRIPTS

#55 Post by Hallvor »

Thanks for helping me improve the script! Ogg Vorbis and Opus are indeed great.
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
sunrat
Administrator
Administrator
Posts: 6412
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 116 times
Been thanked: 461 times

Re: Share your Debian SCRIPTS

#56 Post by sunrat »

Hallvor wrote: 2023-01-11 09:15Edit2: I altered the default bitrate for opus to 256k.
I find opus at 160k to be fairly transparent ie. almost impossible to pick from its lossless source. Vorbis I use q7 or q8 which is ~200k-220k.
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Share your Debian SCRIPTS

#57 Post by Hallvor »

sunrat wrote: 2023-01-11 22:16
Hallvor wrote: 2023-01-11 09:15Edit2: I altered the default bitrate for opus to 256k.
I find opus at 160k to be fairly transparent ie. almost impossible to pick from its lossless source. Vorbis I use q7 or q8 which is ~200k-220k.
Well, 256k may be overkill for opus, but the files I made are still only a few megabytes. (The biggest was 9 megabytes for a five minute song, while the others were about 7 megabytes.) It isn't too bad. But I wouldn't bet that I could hear the difference between 160k and 256k. :lol:
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Share your Debian SCRIPTS

#58 Post by Hallvor »

New version with easier quality selection. Tested and running great.

Code: Select all

#!/bin/bash

read -p "Enter the desired audio format (mp3, opus, ogg): " audio_format

case $audio_format in
    "mp3") read -p "Enter the desired quality (0-9): " quality ;;
    "opus") read -p "Enter the desired bitrate for Opus (64k-256k): " bitrate
           read -p "Enter the desired compression level for Opus (0-10): " compression ;;
    "ogg") read -p "Enter the desired quality (0-9): " quality ;;
esac

read -p "Enter the input directory: " input_dir
read -p "Enter the output directory: " output_dir

if [ ! -d "$output_dir" ]; then
  mkdir -p "$output_dir"
fi

IFS=$'\n'
for file in $(find "$input_dir" \( -name '*.flac' -o -name '*.alac' -o -name '*.wav' \) -type f); do
    relative_path="$(dirname "${file#$input_dir}")"
    output_path="$output_dir/$relative_path"
    mkdir -p "$output_path"
    case $audio_format in
        "mp3") ffmpeg -i "$file" -q:a $quality -map a "$output_path/$(basename "${file%.*}").mp3";;
        "opus") ffmpeg -i "$file" -compression_level $compression -b:a $bitrate -vbr on "$output_path/$(basename "${file%.*}").opus";;
        "ogg") ffmpeg -i "$file" -q:a $quality -map a -c:a libvorbis "$output_path/$(basename "${file%.*}").ogg";;
    esac
done

[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Share your Debian SCRIPTS

#59 Post by Hallvor »

A simple egg cooking timer

Code: Select all

#!/bin/bash

echo "7 minutes. This length allows for a soft, runny yolk and firm white."
echo "8 minutes. The yolk is jammy and soft but not liquid."
echo "10 minutes. The eggs are mostly cooked through but slightly soft in the center."
echo "12–13 minutes. This amount of time will result in fully hard-boiled eggs that are not over-cooked."

read -p "Enter the duration of the timer in minutes: " duration

echo "Timer started for $duration minutes"

# Convert minutes to seconds
duration=$((duration * 60))

# Start the timer
for ((i=$duration; i>=0; i--)); do
    # Print the remaining time in minutes and seconds
    printf "\rTime remaining: %02d:%02d" $((i/60)) $((i%60))
    sleep 1
done

# Play the mp3 file
mplayer /path/to/mp3

echo "Your eggs are ready! Cool down the eggs to halt the cooking process."
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Share your Debian SCRIPTS

#60 Post by Hallvor »

Here is a very simple batch video converter script. I use it to shrink video files to smaller sizes for portable devices with little storage (=my mobile and tablet).

Usage: Set input directory, output directory, codec, resolution and bitrate, and the script will do everything else.

Limitation: The script does not search recursively. All files must be in the input folder.

Code: Select all

#!/bin/bash

# Get the input directory
read -p "Please enter the input directory: " input_dir

# Get the output directory
read -p "Please enter the output directory: " output_dir

# Get the codec
read -p "Please enter the codec (libx264 or libx265): " codec

# Get the target resolution
read -p "Please enter the target resolution (e.g. 720x480, 1280x720 or 1920x1080): " resolution

# Get the target bitrate
read -p "Please enter the target bitrate (e.g. 1000k, 2000k or 4000k): " bitrate

# Check if the input and output directories are provided
if [ -z "$input_dir" ] || [ -z "$output_dir" ]; then
    echo "Both input and output directories are required."
    exit 1
fi

# Check if codec is supported
if [ "$codec" != "libx264" ] && [ "$codec" != "libx265" ]; then
    echo "Codec is not supported, please enter either libx264 or libx265."
    exit 1
fi

# Create the output directory if it does not exist
if [ ! -d "$output_dir" ]; then
    mkdir -p "$output_dir"
    echo "Output directory created"
fi

# Save the current IFS
OLDIFS=$IFS
IFS=$'\0'

# Find all the video files in the input directory
for file in "$input_dir"/*; do
    # Get the filename without the path
    filename=$(basename "$file")

    # Use ffmpeg to convert the video file to the smaller size
    ffmpeg -i "$file" -s $resolution -b:v $bitrate -c:v $codec -c:a copy -map 0 -scodec copy -strict -2 "$output_dir/$filename"
done

# Print a message to confirm the process is complete
echo "Batch video conversion complete!"

# Reset the IFS
IFS=$OLDIFS
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

Post Reply