ssec / polar2grid

Tools for reading, remapping, and writing satellite instrument data.
http://www.ssec.wisc.edu/software/polar2grid/
GNU General Public License v3.0
72 stars 34 forks source link

Geo2Grid additional option added to gtiff2mp4.sh #345

Open jpnIII opened 3 years ago

jpnIII commented 3 years ago

Currently, the gtiff2mp4.sh Geo2Grid bash script does not have an option to specify the mp4 loop speed. A default value of 24 frames per second is assigned within the script. This note is just to request that the next release of Geo2Grid include an updated version of gtiff2mp4.sh that allows the user to specify a desired loop speed on the gtiff2mp4.sh commandline.

I have updated the 1.0.2 version of gtiff2mp4.sh on our system (imaginator.ssec.wisc.edu) to do what I am suggesting. The updated script has been placed onto the SSEC anonymous ftp server (ftp.ssec.wisc.edu, 128.104.108.86) at:

   /pub/imaginator/users/jpn/for_dave_hoese/gtiff2mp4_jpn.sh

A user can enter either an "X" or an "x" for input argument #2 to cause the default loop speed of 24 to be used. I am also echoing to the screen the ffmpeg command that gets executed within gtiff2mp4.sh. That output was for our benefit in Room 211 -- Go ahead and remove it if you wish!

And I am also including the updated version of gtiff2mp4.sh immediately below. Thank you! Sincerely, Jim

!/usr/bin/env bash

encoding: utf-8

Usage: gtiff2mp4.sh output.mp4 input1.tif input2.tif ...

Copyright (C) 2014 Space Science and Engineering Center (SSEC),

University of Wisconsin-Madison.

#

This program is free software: you can redistribute it and/or modify

it under the terms of the GNU General Public License as published by

the Free Software Foundation, either version 3 of the License, or

(at your option) any later version.

#

This program is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

GNU General Public License for more details.

#

You should have received a copy of the GNU General Public License

along with this program. If not, see http://www.gnu.org/licenses/.

#

This file is part of the polar2grid software package. Polar2grid takes

satellite observation data, remaps it, and writes it to a file format for

input into another program.

Documentation: http://www.ssec.wisc.edu/software/polar2grid/

#

Written by David Hoese November 2018

University of Wisconsin-Madison

Space Science and Engineering Center

1225 West Dayton Street

Madison, WI 53706

david.hoese@ssec.wisc.edu

#

dquote=\"

if [ -z "$POLAR2GRID_HOME" ]; then export POLAR2GRID_HOME="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )" fi

Setup necessary environments

source $POLAR2GRID_HOME/bin/env.sh set -e

if [ $# -lt 3 ]; then

&2 echo "Usage: gtiff2mp4.sh output.mp4 frames_per_second input1.tif input2.tif" exit 1 fi

TMP_FRAME_DIR="gtiff2mp4_tmp"

OUTPUT_FILENAME="$1" # Get the name of the output mp4 loop via input argument #1. shift # Shift all commandline arguments one argument to the left. The original first argument goes off the left hand side.

IN_FPS="$1" # Get the input frames per second desired for the output mp4 loop via input argument #1.

if [ "${IN_FPS}" = "X" -o "${IN_FPS}" = "x" ]; then FPS=24 elif [ ${IN_FPS} -le 0 ]; then

&2 echo "ERROR: Enter a positive integer for frames_per_second Usage: gtiff2mp4.sh output.mp4 frames_per_second input1.tif input2.tif" exit 1 else FPS=${IN_FPS} fi shift # Shift all commandline arguments one argument to the left. The original first argument goes off the left hand side.

INPUT_FILES=( "$@" ) # The files to be written to the mp4 loop are all remaining commandline arguments.

MAX_IMG_SIZE=${MAX_IMG_SIZE:-4096}

gdal_size() { if [[ -z "$1" ]]; then echo "Missing arguments. Syntax:" echo " gdal_pixelsize_gdalwarp_tr " return fi; EXTENT=$(gdalinfo "$1" |\ grep "Size is" |\ sed "s/Size is //g; s/,/ /g" |\ tr "\n" " " |\ tr -d "[(,])-"); echo -n "$EXTENT" }

get_new_image_width() { img_width=$1 img_height=$2 if [[ $img_width -ge $img_height ]]; then if [[ $img_width -ge $MAX_IMG_SIZE ]]; then echo $MAX_IMG_SIZE else echo $img_width fi else if [[ $img_height -gt $MAX_IMG_SIZE ]]; then

use ratio to get width

        echo $(( $MAX_IMG_SIZE * $img_width / $img_height ))
    else
        echo $img_width
    fi
fi

}

cleanup() {

&2 echo "Removing temporary directory \"$TMP_FRAME_DIR\"" rm -rf "$TMP_FRAME_DIR" } trap cleanup 0 ERR

echo "Creating temporary directory for sorting frames..." mkdir -p "$TMP_FRAME_DIR"

echo "Creating video file from: " printf '%s\n' ${INPUT_FILES[@]} echo "Total number of input files: ${#INPUT_FILES[@]}"

FILE_EXT=${INPUT_FILES[0]##.}

x=1 echo "Preparing images for video conversion..." for i in "${INPUT_FILES[@]}"; do counter=$(printf %03d $x) img_width_height=gdal_size $i img_width=echo $img_width_height | cut -f1 -d' ' img_height=echo $img_height | cut -f3 -d' ' new_width=get_new_image_width $img_width $img_height if [[ $new_width -eq $img_width ]]; then ln -s "../$i" "${TMP_FRAME_DIR}/${counter}.${FILE_EXT}" else echo "Scaling image to work with ffmpeg (New width=${new_width})" gdal_translate -outsize $new_width 0 $i "${TMP_FRAME_DIR}/${counter}.${FILE_EXT}" fi x=$(($x+1)) done

echo "Generating animation..."

INPUT_PARAMS=${INPUT_PARAMS:--framerate ${FPS} -f image2}

OUTPUT_PARAMS=${OUTPUT_PARAMS:--c:v libx264 -crf 25 -vf "format=yuv420p,scale=trunc(iw/2)2:trunc(ih/2)2"}

cmd="ffmpeg -y $INPUT_PARAMS -i ${dquote}${TMP_FRAME_DIR}/%03d.${FILE_EXT}${dquote} $OUTPUT_PARAMS $OUTPUT_FILENAME"

echo echo "($0) Execute: ${cmd}" echo

ffmpeg -y $INPUT_PARAMS -i "${TMP_FRAME_DIR}/%03d.${FILE_EXT}" $OUTPUT_PARAMS $OUTPUT_FILENAME

echo "Done"

djhoese commented 3 years ago

Thanks @jpnIII. Maybe it would be easiest if I converted this script to use Python and we could have a full, more complex, set of command line options. For this specifically, we could probably allow an environment variable default. This is actually what INPUT_PARAMS is doing right now. You could theoretically do:

$ INPUT_PARAMS="--framerate 48 -f image2}" gtiff2mp4.sh ...

And this would accomplish the same end goal.

jpnIII commented 3 years ago

Hi Dave – Thank you for the quick reply! Yes, I noticed the ${INPUT_PARAMS} variable, and was wondering what you were using it for. But now I see your point. We would certainly be happy if your team found the time/funds to upgrade the script to use Python!

Have a fine rest of your weekend, Dave. Hope all is well at home for everybody. ☺

Sincerely,

Jim

From: David Hoese @.> Sent: Saturday, April 24, 2021 2:00 PM To: ssec/polar2grid @.> Cc: James P. Nelson @.>; Mention @.> Subject: Re: [ssec/polar2grid] Geo2Grid additional option added to gtiff2mp4.sh (#345)

Thanks @jpnIIIhttps://github.com/jpnIII. Maybe it would be easiest if I converted this script to use Python and we could have a full, more complex, set of command line options. For this specifically, we could probably allow an environment variable default. This is actually what INPUT_PARAMS is doing right now. You could theoretically do:

$ INPUT_PARAMS="--framerate 48 -f image2}" gtiff2mp4.sh ...

And this would accomplish the same end goal.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/ssec/polar2grid/issues/345#issuecomment-826138475, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AARELF2P4XUQP6LPPUF473TTKMIJJANCNFSM43QM74MA.

djhoese commented 1 year ago

Unfortunately we won't have time for the G2G 1.1 release to include the changes for this script. We will put it on the TODO list for G2G 1.2. Hopefully the workaround mentioned above with the existing environment variables can be useful enough for users for the time being.

kathys commented 9 months ago

Not addressing this until perhaps the next version.

jpnIII commented 9 months ago

Thank you for your reply, Kathy!

Later.

Sincerely,

Jim From: kathys @.> Sent: Wednesday, January 10, 2024 3:27 PM To: ssec/polar2grid @.> Cc: James P. Nelson @.>; Mention @.> Subject: Re: [ssec/polar2grid] Geo2Grid additional option added to gtiff2mp4.sh (#345)

Not addressing this until perhaps the next version.

— Reply to this email directly, view it on GitHubhttps://github.com/ssec/polar2grid/issues/345#issuecomment-1885759793, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AARELF476KTGMXDM4BIRYFLYN4BQVAVCNFSM43QM74MKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBYGU3TKOJXHEZQ. You are receiving this because you were mentioned.Message ID: @.***>