color


ffmpeg to convert MP4 files to MKV files with the libx265 video codec

When working with video files, there are times when you need to convert them from one format to another or modify them in some other way. One of the most popular tools for this is ffmpeg. This command-line tool can do a lot of things related to video processing, including conversion, resizing, cropping, and more. In this blog post, we will explain a script that uses ffmpeg to convert MP4 files to MKV files with the libx265 video codec.

The Script:

Here’s the script that we will be explaining:

for FILE in *.mp4; do
  echo -e "Processing video '\e[32m$FILE\e[0m'";
  ffmpeg -i "${FILE}" -analyzeduration 2147483647 -probesize 2147483647 -c:v libx265 -an -x265-params crf=0 "${FILE%.mp4}.mkv";
done;

Let’s break this script down line by line to understand what it does.

for FILE in *.mp4; do
...
done;

This line starts a loop that goes through all the files in the current directory that have the “.mp4” extension. The loop will execute the commands inside the “do” and “done” keywords for each file that matches this pattern.

echo -e "Processing video '\e[32m$FILE\e[0m'";

This line uses the “echo” command to print a message to the console. The message includes the file’s name being processed, which is stored in the $FILE variable. The “\e[32m” and “\e[0m” are escape sequences that change the color of the text to green. This is just a way to make the message more visually appealing.

ffmpeg -i "${FILE}" -analyzeduration 2147483647 -probesize 2147483647 -c:v libx265 -an -x265-params crf=0 "${FILE%.mp4}.mkv";

This line runs the ffmpeg command to convert the current file from MP4 to MKV format using the libx265 video codec. Let’s break down each of the options:

  • “-i ${FILE}” specifies the input file. “${FILE}” is the name of the file being processed, which is stored in the $FILE variable.
  • “-analyzeduration 2147483647” and “-probesize 2147483647” are options that tell ffmpeg to analyze the entire file before starting the conversion process. This can help avoid some issues that can occur when processing large files.
  • “-c:v libx265” specifies the video codec for the output file. libx265 is a popular video codec that provides good quality at a smaller file size.
  • “-an” specifies that there should be no audio in the output file.
  • “-x265-params crf=0” sets the quality level for the video. A value of 0 means lossless compression, which is the highest quality possible.
  • “${FILE%.mp4}.mkv” specifies the name of the output file. “${FILE%.mp4}” removes the “.mp4” extension from the input file name, and “.mkv” adds the “.mkv” extension to the end.

Conclusion:

The script we’ve just explained is a simple example of how you can use ffmpeg to convert MP4 files to MKV files with the libx265 video codec. It uses a loop to process all the files in the current directory that match the “.mp4” pattern. The script also prints a message to the console for each file


cecho – a function to print using different colors in bash

The following script can be used to print colored text in bash.
You can use it in any script without copy pasting everything in it by executing the following command source cecho.sh.
Doing so, it will load to your script the functions that are defined in cecho.sh, making them available for you to use (something like including code in C, with some caveats).

[download id=”2113″]

#!/bin/bash

# The following function prints a text using custom color
# -c or --color define the color for the print. See the array colors for the available options.
# -n or --noline directs the system not to print a new line after the content.
# Last argument is the message to be printed.
cecho () {

    declare -A colors;
    colors=(\
        ['black']='\E[0;47m'\
        ['red']='\E[0;31m'\
        ['green']='\E[0;32m'\
        ['yellow']='\E[0;33m'\
        ['blue']='\E[0;34m'\
        ['magenta']='\E[0;35m'\
        ['cyan']='\E[0;36m'\
        ['white']='\E[0;37m'\
    );

    local defaultMSG="No message passed.";
    local defaultColor="black";
    local defaultNewLine=true;

    while [[ $# -gt 1 ]];
    do
    key="$1";

    case $key in
        -c|--color)
            color="$2";
            shift;
        ;;
        -n|--noline)
            newLine=false;
        ;;
        *)
            # unknown option
        ;;
    esac
    shift;
    done

    message=${1:-$defaultMSG};   # Defaults to default message.
    color=${color:-$defaultColor};   # Defaults to default color, if not specified.
    newLine=${newLine:-$defaultNewLine};

    echo -en "${colors[$color]}";
    echo -en "$message";
    if [ "$newLine" = true ] ; then
        echo;
    fi
    tput sgr0; #  Reset text attributes to normal without clearing screen.

    return;
}

warning () {

    cecho -c 'yellow' "$@";
}

error () {

    cecho -c 'red' "$@";
}

information () {

    cecho -c 'blue' "$@";
}

 

Usage

Function cecho accepts the options to set the color and to control if a new line should be print.
Parameter -c or --color define the color for the print. See the array colors for the available options.
Parameter -n or --noline directs the system not to print a new line after the content.
The last parameter is the string message to be printed.
Functions warning, error and information are using cecho to print in color.
These three functions always print a new line and they have hardcoded one color set for each.

Example


#Get the name of the script currently being executed

scriptName=$(basename $(test -L "$0" && readlink "$0" || echo "$0"));

#Get the directory where the script currently being executed resides

scriptDirDIR=$(cd $(dirname "$0") && pwd);

#Print in blue color with no new line

cecho -n -c 'blue' "$scriptDir";

#Print in red color with a new line following the message

cecho -c 'red' "$scriptName";

#Using the information() function to print in blue followed by a new line

information ‘End of script’;


Bash Function to print out the files and the lines that contain a needle

The following code will create a function in bash that accepts two parameters (1: the place to search in, 2: the value to search for).

You can place it in your ~/.bashrc file to have it available whenever you open a bash shell.

#1. Copy/paste the below lines in your .bashrc

#takes 2 parameters (1: the haystack to search in, 2: the needle)
# Will print out the files and the lines that contain the needle
xfind(){
  FIND_VAR="$2";
  STACK="$1";
  if [ -f "$STACK" ] || [ -d "$STACK" ]; then
    find "$STACK" \
      -exec grep --color "$FIND_VAR" -sl '{}' \; \
      -exec grep "$FIND_VAR" -s '{}' \;
  else
    echo "ERROR: No file or folder with the name '$STACK' exist";
  fi
}
#2. Run source ~/.bashrc -- to reload 

Usage examples:

xfind . "bar";
xfind /etc/ "conf";

Tail logs with color for Monolog 1

#1. Copy/paste the below lines in your .bashrc

tailf-with-colors () {
    if [ -z "$1" ] ; then
        echo "Please specify a file for monitoring"
        return
    fi

    tail -f $1 | awk '
                {matched=0}
                /INFO:/    {matched=1; print "\033[0;37m" $0 "\033[0m"}   # WHITE
                /NOTICE:/  {matched=1; print "\033[0;36m" $0 "\033[0m"}   # CYAN
                /WARNING:/ {matched=1; print "\033[0;34m" $0 "\033[0m"}   # BLUE
                /ERROR:/   {matched=1; print "\033[0;31m" $0 "\033[0m"}   # RED
                /ALERT:/   {matched=1; print "\033[0;35m" $0 "\033[0m"}   # PURPLE
                matched==0            {print "\033[0;33m" $0 "\033[0m"}   # YELLOW
        '
}

#2. Run source ~/.bashrc -- to reload 

#3. Run tailf-with-colors <filename>