Sponsored Content
Top Forums Shell Programming and Scripting Function to get the duration of all videos in a folder(s) Post 303016233 by zouhair on Monday 23rd of April 2018 01:33:47 AM
Old 04-23-2018
Function to get the duration of all videos in a folder(s)

did this function to generate the duration of all the video files in a folder or multiple folders, it works fine for my use (I am no Guru as you may have noticed) but when I give it a lot of folders the calculation get a bit borked.


If any good soul had the energy to look at it and give pointers I will be grateful, but you really don't have to Smilie


Code:
vidlength ()
{
    back=$(pwd)
    folder_length=
    total_folder_length=
    array=()

    # Look at the root of the folder
    # https://stackoverflow.com/a/23357277 How can I store find command result as arrays in Bash
    # $REPLY is the default variable when 'read' is not given one
    while IFS=  read -r -d $'\0'; do
        array+=("$REPLY")
    done < <(find . -maxdepth 1 \( -iname '*.mkv' -o -iname '*.mp4' -o -iname '*.avi' \) -print0)

    # exiftool -n -q -p '$Duration#' "${array[@]}" 2> /dev/null | awk '{sum += $0}; END{print sum}'                     GIVES TIME AS 78682.8
    # exiftool -n -q -p '${Duration;our $sum;$_=ConvertDuration($sum+=$_)}' "${array[@]}" 2> /dev/null| tail -n1        GIVES TIME AS 0:21:51:22
    
    # This command gives a result of '159' if the array is empty (2 minutes 39 seconds)
    total_folder_length=$(exiftool -n -q -p '$Duration#' "${array[@]}" 2> /dev/null | awk '{sum += $0}; END{print sum}') 

    # I have to add this because for wathever reason the last command fills len with the number 159 when it finds no video in the folder or when the array is empty
    if [[ "${total_folder_length%.*}" -eq 159 ]]; then
        total_folder_length=0
    fi

    counter=$(echo ${#array[@]})

    for folder in ./*; do
        #find . -maxdepth 1 \( -iname '*.mkv' -o -iname '*.mp4' -o -iname '*.avi' \)
        if [ -d "$folder" ]; then
            array=() # Empty the array between loops
            while IFS=  read -r -d $'\0'; do
                array+=("$REPLY")
            done < <(find "$folder" \( -iname '*.mkv' -o -iname '*.mp4' -o -iname '*.avi' \) -print0)
            
            # https://unix.stackexchange.com/a/170973 Way faster with exiftool
            # I have to add this because for wathever reason the last command fills len with the number 159 when it finds no video in the folder or when the array is empty
            folder_length=$(exiftool -n -q -p '$Duration#' "${array[@]}" 2> /dev/null | awk '{sum += $0}; END{print sum}')
            if [[ "${folder_length%.*}" -eq 159 ]]; then
                #len=$(echo "$len-159+$(exiftool -n -q -p '$Duration#' "${array[@]}" 2> /dev/null | awk '{sum += $0}; END{print sum}')" | bc)
                continue
            else
                total_folder_length=$(echo "$total_folder_length+$(exiftool -n -q -p '$Duration#' "${array[@]}" 2> /dev/null | awk '{sum += $0}; END{print sum}')" | bc)
            fi

            counter=$(echo $(echo ${#array[@]})+$counter | bc)
        fi
    done

    # Change it from 78682.8 to 0:21:51:22 https://unix.stackexchange.com/a/34033
    total=$(echo $total_folder_length | awk '{printf("%d:%02d:%02d:%02d\n",($1/60/60/24),($1/60/60%24),($1/60%60),($1%60))}')
    days=$(echo ${total} | awk -F ":" '{print $1}')
    hours=$(echo ${total} | awk -F ":" '{print $2}')
    minutes=$(echo ${total} | awk -F ":" '{print $3}')
    seconds=$(echo ${total} | awk -F ":" '{print $4}')

    if [[ "${days#0}" -gt 0 ]]; then
        #printf "The total duration found is:\n"
        printf "%d day(s) %02d hour(s) %02d minute(s) and %02d second(s) in %d videos\n" "${days#0}" "${hours#0}" "${minutes#0}" "${seconds#0}" "$counter"
    else
        if [[ "${hours#0}" -gt 0 ]]; then
            #printf "The total duration found is:\n"
            printf "%02d hour(s) %02d minute(s) and %02d second(s) in %d videos\n" "${hours#0}" "${minutes#0}" "${seconds#0}" "$counter"
        else
            #printf "The total duration found is:\n"
            printf "%02d minute(s) and %02d second(s) in %d videos\n" "${minutes#0}" "${seconds#0}" "$counter"
        fi
    fi

    cd "$back"
}

 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

duration calculation

I have a file which has 3 coloumns emp_name, Joining_date, Designation. abc 12/1/2001 SSE def 2/25/2007 SE ghi 3/18/2009 SA abc 8/1/2008 SSE def 2/13/2007 SE ghi 3/24/2005 SA I need to find out the emp who has been in the company for longest period(Till date). Can I have any... (3 Replies)
Discussion started by: siba.s.nayak
3 Replies

2. Solaris

ufsdump backup duration

hi, i'm trying to figure out how to tell the amount of time a ufsdump of a directory takes. i use the below command: echo "Starting Backup of u4" >> /backup/backup.log 2>&1 /usr/sbin/ufsdump 0uf /dev/rmt/0n /u4 >> /backup/backup.log 2>&1 echo "Finished Backup of u4" >> /backup/backup.log... (0 Replies)
Discussion started by: pinoy43v3r
0 Replies

3. Shell Programming and Scripting

Get password protected URL folder using PHP fopen function

Hi everybody, Please some help over here, I`m pretty new in PHP. I have a cronrefresh php file within a website, I need this script get infornation from a URL of the site. Part of the script where $URL variable appears is: $fdURL = mysql_query("SELECT * FROM affiliSt_config WHERE name... (2 Replies)
Discussion started by: cgkmal
2 Replies

4. UNIX for Dummies Questions & Answers

Copy duration of cp

Hello forum, i would like to ask if there's a way to view the remaining time of copying files (talking about copying gigabytes) while the cp commnad is running. I'm using OpenBSD 4.9 -stable. Thanx in advance. :) (2 Replies)
Discussion started by: sepuku
2 Replies

5. Shell Programming and Scripting

Convert duration of the process to seconds

Hi, I am looking to write a script to kill the process which are running for more than 7 days. So i have a command like "ps -eo pid,etime,args | grep -i xxxx" ( process which has xxx in it and running for more than 7 days needs to be killed ). When i exeucte the above command , i am... (2 Replies)
Discussion started by: forums123456
2 Replies

6. UNIX Desktop Questions & Answers

arecord not interrupted after specified duration

I have used the arecord command like this arecord -d 1 test.wav It is keep on waiting. I need to manually interrupt it by ctrl-c. Why it is not interrupting after one second? The arecord version which I am using is : arecord: version 1.0.23 by Jaroslav Kysela (3 Replies)
Discussion started by: thillai_selvan
3 Replies

7. Shell Programming and Scripting

Sort by Duration

.......................................................................................................................... 03:40 Geonetric File from CCL Complete 03:40:59 03:41:08 00:00:09 00:00:01 N/A 005 sys_runccl ... (7 Replies)
Discussion started by: Daniel Gate
7 Replies

8. UNIX for Beginners Questions & Answers

Process duration

Hi , How can I check that for a single process, for example pagent for how much duration this process was up or down and also I need multiple entries if this process was down or up multiple times. Please help. (3 Replies)
Discussion started by: Ashish Garg
3 Replies

9. Shell Programming and Scripting

Duration Calculation

I have 2 variables startTime='122717 23:20' endTime='122817 0:40' how can i get the elapsed duration as like "1 hour 20 minutes" ? (8 Replies)
Discussion started by: vikram3.r
8 Replies
All times are GMT -4. The time now is 02:43 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy