3

Basically I'm Try to edit a bash script. To goal of the Script is to allow me to take a bunch of screenshot of videos by Simply launching it from terminal with only having to specify the Filename and the Number screenshot desired. I've worked out a few bugs but I'm having issues with getting MPV to assign the filename I want.

Here's the script as it stands currently.

#!/bin/bash


### Global variables
filename="$1"

### Error handling
if [ -z ${filename} ]; 
then
    echo "ERROR: No video file supplied. Please enter a video file as argument."
    exit 1;
fi

NUM_OF_SCREENSHOTS=9
if [ ! -z $2 ]; 
then
  NUM_OF_SCREENSHOTS=$2
  echo "WARNING: Overwrite default number of screenshots to ${NUM_OF_SCREENSHOTS}."
  sleep 3s
fi




# Get the total length of the video in seconds.
#  Use mplayer to display the info of the video and then get the value of ID_LENGTH, the total number of seconds of the video.
total_length=$(mplayer -identify -frames 0 -vc null -vo null -ao null "$filename" | grep ID_LENGTH | sed 's/ID_LENGTH=//' | sed 's/\..*//')
# Reference https://github.com/mpv-player/mpv/blob/master/TOOLS/mpv_identify.sh

# Remove 4 seconds from the video so that it doesn't take screenshot at the ends.
let total_length-=4

# time_slice: At which time interval should mplayer take screenshots.
let time_slice=${total_length}/${NUM_OF_SCREENSHOTS}

# time_at: When should mplayer take screenshots.
time_at=${time_slice};

# Looping to take screenshots.
for ((i=1; i <= NUM_OF_SCREENSHOTS ; i++))
do


  # Take the screenshot.
  #mplayer -loop 1 -nosound -frames 1 -ss ${time_at} -vo png:z=9 ${filename}
  mpv --quiet --no-audio --vo=image --screenshot-template="%f %n" --start=${time_at} --frames=1 "$filename" 

  # Increment to the next time slice.
  let time_at+=${time_slice}



done
5
  • The script looks good (haven't tried it) but to what filename are you referring in your question ? Surely not the $filename parameter in the script ? Can you clarify ?
    – Cbhihe
    Oct 13, 2015 at 6:31
  • Do you have a message after each screenshot like Exiting... (End of file)?
    – A.B.
    Oct 13, 2015 at 6:57
  • Well I eventually got it to do what i wanted on my own. I simply needed a way to create unique filenames for the Images created because otherwise rerunning the script always overwrote the last set. This issue was moving 0000001.jpeg to a unique filename so it woulnd be overwrote. I eventually used this line to solve it mv "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/"00000001.* ~/Pictures/${padding}.$$ I made a few other changes to it as made obvius by the extra additions there. I
    – wisperer
    Oct 14, 2015 at 6:31
  • A.B. I did but when the script was running (before i fixed it) the next screenshot would fire off before mv could trigger so i had to add a few stops to it. otherwise the end line wouldn't show as it didn't run right. that's what eventually helped me work out most of the bugs.
    – wisperer
    Oct 14, 2015 at 6:42
  • If the answer helped you, don't forget to click the grey at the left of its text, which means Yes, this answer is valid! ;-)
    – Fabby
    Oct 15, 2015 at 10:01

1 Answer 1

1

The script can't work in this way. The script is mostly fine, although you should make minor improvements (Use ShellCheck for that.)

The problem is, mpv uses an internal counter for the naming of the screenshot file (%n). And the counter is reseted at each pass of the loop.

The modified script below renames the file.

#!/usr/bin/env bash

### Global variables
filename="$1"

### Error handling
if [ -z "${filename}" ]; 
then
    echo "ERROR: No video file supplied. Please enter a video file as argument."
    exit 1;
fi

NUM_OF_SCREENSHOTS=9
if [ ! -z "$2" ]; 
then
  NUM_OF_SCREENSHOTS=$2
  echo "WARNING: Overwrite default number of screenshots to ${NUM_OF_SCREENSHOTS}."
  sleep 3s
fi

# Get the total length of the video in seconds.
#  Use mplayer to display the info of the video and then get the value of ID_LENGTH, the total number of seconds of the video.
total_length=$(mplayer -identify -frames 0 -vc null -vo null -ao null "$filename" | grep ID_LENGTH | sed 's/ID_LENGTH=//' | sed 's/\..*//')
# Reference https://github.com/mpv-player/mpv/blob/master/TOOLS/mpv_identify.sh

# Remove 4 seconds from the video so that it doesn't take screenshot at the ends.
let total_length-=4

# time_slice: At which time interval should mplayer take screenshots.
let time_slice=${total_length}/${NUM_OF_SCREENSHOTS}

# time_at: When should mplayer take screenshots.
time_at=${time_slice};

# Looping to take screenshots.
for ((i=1; i <= NUM_OF_SCREENSHOTS ; i++))
do

  # Take the screenshot.
  #mplayer -loop 1 -nosound -frames 1 -ss ${time_at} -vo png:z=9 ${filename}
  mpv --quiet --no-audio --vo=image --start=${time_at} --frames=1 "$filename" 
  rename 's/^[0-9]+/out'"${time_at}"'/' 00000001.jpg

  # Increment to the next time slice.
  let time_at+=${time_slice}

done

exit 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.