-2

I am running this statement in shell

for file in /path/to/files/*.mp4; do ffprobe -show_frames $file | grep "pkt_size" > ${file}.txt

done

It would be great if I could get this information too;

  1. When I grep pkt_size row with numbers then I want script to - divide every pkt_size number with 100,
  2. then add (sum) pkt_size row numbers every after 50 rows.
Ben
  • 91
  • 1
  • 2
  • 9
  • 1. There is no `done` at the end of `for` loop. 2. Use correct formatting for source code. – Arkadiusz Drabczyk May 07 '15 at 14:03
  • There is no "data" filed in -show_frames output. Which field do you want to grep? If you can explain what exactly you want to do then may be can suggest different approach. – Alam May 07 '15 at 14:15
  • well greping the data was an example - I have edited the script again. 1) Search all the mp4s in the folder, 2) Do ffprobe -show_frames 3) Create individual output file for every video in the folder 4) Take the "pkt_size" row from the output 5) Add(sum) pkt_size numbers after every 50 rows (set of 50 rows) 6) finally, divide by 1000 with every sum(step 5) – Ben May 07 '15 at 14:46
  • 1
    No need for `grep`: `ffprobe -v error -show_entries frame=pkt_size -of default=noprint_wrappers=1:nokey=1 input.mp4` – llogan May 07 '15 at 18:47
  • Lord could you explain little bit pls. Just don't understand how it's adding 50 rows and then diving ..? – Ben May 07 '15 at 20:15

1 Answers1

1

I already showed you in another answer how to iterate all files and append results. This is how you would go about calculating stuff in bash for one file, using @LordNeckbeard's ffprobe command.

Your homework is to modify and integrate the two answers.

#!/bin/bash

file=/path/to/file.mp4

if [ ! -f $file ]; then
    echo Error: File not found
    exit 1
fi

pkt_sizes=$(ffprobe -show_entries frame=pkt_size -of default=noprint_wrappers=1:nokey=1 -i $file -loglevel 0)

function print_sum {
    #use `bc` for floating point operations 
    sum=$(echo "scale=${precision};${sum}/100" | bc -l)
    echo "SUM("$start","$i")="$sum
}

i=1
start=1
sum=0
precision=3

#compute/print sum every 50 packets
for size in $pkt_sizes; do  
    (( sum+= size ))

    if (( $i % 50 == 0 )); then     
        print_sum
        sum=0
        start=$(( i + 1 ))
    fi 

    (( i++ ))   
done;

#sum last packets (n < 50)
if (( "$start" < "$i" )); then  
    (( i-- ))
    print_sum
fi

This is more than enough to get you started with shell arithmetic so please refer to the documentation for further questions.

Community
  • 1
  • 1
aergistal
  • 29,947
  • 5
  • 70
  • 92
  • Hi Aergistal, Many thanks for your efforts. Really appreciate it. I am really new to this shell programming. That's please ignore my silly questions. I saved your code with correct path/to/file/file.mp4 But this is what happenning, Ben@ben-VirtualBox ~/Downlaods $ which bash /bin/bash Ben@ben-VirtualBox ~/Downlaods $ chmod +x script.sh Command not found Ben@ben-VirtualBox ~/Downlaods $ script.sh Command not found – Ben May 08 '15 at 13:19
  • Please guide for execution this particular file. – Ben May 08 '15 at 13:33