2
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
        me= $( ffmpeg -i ${LINE} 2>&1 | grep Duration | sed 's/Duration: \(.*\), start/\1/g' > uber.txt )
    echo "${me}"
done < me.txt

I want to make a script to check all .mp4 files (that are in me.txt) for duration. It actually works but i dont know why it doesnt show duration, it just prints empty lines

  • 3
    You must not put spaces around the `=` in a variable assignment: `me=$( ...` – glenn jackman Dec 05 '20 at 02:23
  • yeah i wrote it in bad style, anyway after it ill give vars uppercase names and remove some extra spaces – Uber Sholder Dec 05 '20 at 02:49
  • Don't use ALLCAPS variable names, leave those as reserved by the shell. One day you'll write `PATH=something` and then [wonder why](https://stackoverflow.com/q/27555060/7552) [your script is broken](https://stackoverflow.com/q/28310594/7552). – glenn jackman Dec 05 '20 at 03:19
  • The purpose of braces is to protect the variable name when concatenating, for example, `baz=${foo}bar`. If you have an invalid character dividing the variable, there is no need for braces `foo$bar`, `$foo/bar` or `foo $bar baz`. –  Dec 05 '20 at 07:10
  • `FFmpeg -i "$LINE" 2>&1` probably a good idea to use quotes on the filename. Think you mean to do ```&& [ -n "$LINE" ]; do```, with `||`, the statement won't be evaluated if `read` is successful. (`read` assigns the value to `$REPLY`, so there is no reason to supply a variable name either if you don't intend to use it further). –  Dec 05 '20 at 09:28
  • puh, next I think you are trying to do `b=$( { ls . nonexistant | cat > uber.txt; } 2>&1 )`, basically everything successful is redirected to uber.txt and all errors are placed in `b` –  Dec 05 '20 at 09:59

1 Answers1

1

0 - I've done something pretty close to what you want.
See : https://askubuntu.com/a/1163082/77093

1 - Get rid of spaces in filenames

IFS=$'\n'
while read -r LINE || [ -n "${LINE}" ]; do
  # ...
done < me.txt

2 - you'd better use the mediainfo program as it outputs the video file duration in miliseconds.

sudo apt install mediainfo

Then

IFS=$'\n'
while read -r Line || [ -n "${Line}" ]; do
  # Test if file is video
  buff=$(file -N -i "${Line}" | grep -E 'video')
  if [ ! -z  "$buff" ]
    then
      MediaDuration=$(mediainfo --Output='General;%Duration%' "${Line}")
      # Want total duration of listed files?
      TotalDuration=$((TotalDuration + MediaDuration))
      # Want total number of video files?
      NbMedia=$((NbMedia + 1))
  fi
done < me.txt

# Format Duration: milliseconds to H:M:S
Seconds=$((TotalDuration / 1000))
FormattedDuration=$(printf '%02dh:%02dm:%02ds\n' $(($Seconds/3600)) $(($Seconds%3600/60)) $(($Seconds%60)))

# Build report
ReportText="${NbFiles} File"
test $NbFiles -gt 1 && ReportText="${ReportText}s"
ReportText="${ReportText} selected\n"
test $NbMedia -gt 0 && ReportText="${ReportText}${NbMedia} Media file" || ReportText="${ReportText}No media file"
test $NbMedia -gt 1 && ReportText="${ReportText}s"
test $NbMedia -gt 0 && ReportText="${ReportText}\nTotal duration: ${FormattedDuration}"
cmak.fr
  • 8,411
  • 2
  • 29
  • 45