Jump to content

Scripting help?


wa4chq

Recommended Posts

Great....! I only know how to write very basic, simple scripts. What I've done in the past is find one that sorta does what I want and then make changes to have it do what I want. I'm not having any luck with this video to image script. What I'd like it to do is:

--ask me for a video clip

--create and name a folder where it will dump or output a set amount of frames as a *.png from the video file.

I found this command that will do part of it if you add the video file. It doesn't create a folder and it dumps all the images into the working directory.

ffmpeg -i (insert_video_clip_here) -r 3 -f image2 foo-%04d.png

Here is what I have so far:

#!/bin/bash
### EXTRACT A SET AMOUNT OF IMAGES FROM VIDEO
### CONVERT TO *.png
*** DUMP INTO NEWLY CREATED FOLDER
echo ""
echo "*** Video to images ***"
echo "-----------------------------"
echo "INSERT VIDEO"
echo "-----------------------------"
echo "*** Create Folder "*" ***"
mkdir ~/video_dump ### not sure how to create folder
### not sure how to add video to this command
### not sure how to dump(output) images to folder
ffmpeg -i *.* -r image4 foo-%04d.png $VIDEO | /video_dump
### 		 \___not sure how to input video file here

O.K....not sure if this makes sense what I'd like to do....

 

Thanks

Thanks

Edited by wa4chq
Link to comment
Share on other sites

OK....am sorta getting somewhere. I'm able to make a folder for the png's and output to it....then I can remove the png's from parent folder. But now I can't get "echo" to print....

I manually inserted the video until I can figure out how to be asked for it....

#!/bin/bash
###  VIDEO TO GIF
echo ""
echo "*** CONVERT VIDEO TO GIF PIX ***"
echo "----------------------------------------"
echo "INSERT VIDEO"
echo "----------------------------------------"
newfolder=vid
mkdir "$newfolder"
ffmpeg -i *.AVI -r 3 -f image2 foo-%04d.png
cp *.png ~/scripts/testing/vid
rm -i *.png
done

Link to comment
Share on other sites

ok.....getting closer....

#!/bin/bash
### VIDEO TO PNG
function press_enter
{
echo ""
echo -n "Press Enter to continue"
read
clear
}
selection=
until [ "$selection" = "0" ]; do
echo ""
echo "*** CONVERT VIDEO TO PNG PIX ***"
echo "----------------------------------------"
echo "1 - INSERT VIDEO"
echo "----------------------------------------"
echo ""
echo -n "Enter selection: "
read selection
echo ""
case $selection in
1 ) ffmpeg -i *.AVI -r 3 -f image2 foo-%04d.png
#newfolder=vid
#mkdir "$newfolder"
#ffmpeg -i *.AVI -r 3 -f image2 foo-%04d.png
#cp *.png ~/scripts/testing/vid
#rm -i *.png
#* ) echo "Please enter 1"; press_enter
esac
done

 

Echo works and command runs. Will continue messing with it.... kinda fun!

Edited by wa4chq
  • Like 1
Link to comment
Share on other sites

Alright, here's what I'm doing....cleaning it up a little........

#!/bin/bash
### VIDEO TO PNG
function press_enter
{
echo ""
echo -n "Press Enter to continue"
read
clear
}
selection=
until [ "$selection" = "0" ]; do
echo ""
echo "*** CONVERT VIDEO TO PNG PIX ***"
echo "----------------------------------------"
echo "1 - INSERT VIDEO"
echo "0 - QUIT"
echo "----------------------------------------"
echo ""
echo -n "Enter selection: "
read selection
echo ""
case $selection in
1 ) newfolder=vid
mkdir "$newfolder"
ffmpeg -i *.AVI -r 3 -f image2 foo-%04d.png
cp *.png ~/scripts/testing/vid
rm -I *.png
esac
done

Edited by wa4chq
Link to comment
Share on other sites

securitybreach

Sorry I haven't jumped back in but I am the middle of trying to remotely fix my cousin's machine but he doesn't have ssh running nor does he know how to start it.

  • Like 1
Link to comment
Share on other sites

Well just about.... did this so I can enter what the *.AVI or whatever format the video is....

#!/bin/bash
###  VIDEO TO PNG
function press_enter
{
echo ""
echo -n "Press Enter to continue"
read
clear
}
selection=
until [ "$selection" = "0" ]; do
echo ""
echo "*** CONVERT VIDEO TO PNG's ***"
echo "----------------------------------------"
echo "1 - INSERT VIDEO : "
echo "0 - EXIT"
#read r
echo "----------------------------------------"
echo ""
echo -n "Enter selection: "
read selection
#echo ""
read r
case $selection in
1 ) newfolder=vid
mkdir "$newfolder"
ffmpeg -i $r -r 5 -f image2 foo-%04d.png
cp *.png ~/scripts/testing/vid
rm -I *.png
esac
done

I have a video file in the home directory where I've been testing this script. It asks for the file and accepts it, runs the programs and prompts me about removing the *.png files in home directory.....all good. Only issue is when I want to exit. I'm shown the options 1 or 0, it asks to enter selection (0 to exit), pressing 0 actually requires me to enter 0 again and then it exits.....if that is all the problems I have I'm ok with that!

  • Like 1
Link to comment
Share on other sites

NIcely done

Thanks.....but again, I really don't know what it is I'm doing. I just find things that are close and then keep plugging away, look at results, more messing.... a lot of trial and error (mostly errors >_< ) I'm really excited that I sorta got it to do what I wanted!

 

The next thing to do is trick it out..... gotta have some colors!! :teehee:

  • Like 2
Link to comment
Share on other sites

I have modified scripts in the past, but not much...and I mostly break them before I fix them. You are way more advanced than I am! But IMO, this is screaming to be done with Zenity or YAD! It's been a year or longer, but I've used YAD to create my AIO Control Panel for my LXDE/Lubuntu remix, which assembles all of the various LXDE settings in one place. FWIW, you could create an "input GUI" to input your clip, state the number of clips to extract, state and/or create the destination folder, etc...

 

This link is kind of dated (from 2014) and I'm sure YAD has added features and more variable types since then, but I found it quite helpful to get started. Might even be enough for you to do EXACTLY what you described above!

 

http://www.thelinuxr...-entry-with-yad

 

While the link above was helpful in learning what YAD could do, this link to the manpage was helpful to figure out how to get YAD to do what I WANTED it to do:

 

https://www.mankier.com/1/yad

 

Between these 2 links, I think you might have everything you need! I have no use for your script, but I would absolutely love to see a screenie of your finished project IF you go this route! FWIW...

Edited by Hedon James
  • Like 2
Link to comment
Share on other sites

Thanks Hedon James..... and thanks for the links. I'll check it out. What I enjoy doing is graphics which includes animated gifs. If I use my video or one ripped from youtube, I wanted a way to extracted x-amount of images from an x-amount of video. Once that is done, I finish the project using Gimp and over-lay my images onto the extracted images....... so the little script just helps me.....

Edited by wa4chq
Link to comment
Share on other sites

Sounds like YAD would be perfect for this! When you're done, you could offer up your script s a GUI program, called Animated GIF-creator, orr somethng ssimilar!

Link to comment
Share on other sites

I looked at yad....tell you the truth, I'm not sure what I'm looking at. Installed the slack-build.....I'm thinking my little script as is, is all it will be..... :whistling:

Link to comment
Share on other sites

Also, the script isn't for animating....I use gimp for that because I'm editing the pictures I've grabbed from the video, then save as an animated gif..... the script just grabs images from a video file.....

Link to comment
Share on other sites

I looked at yad....tell you the truth, I'm not sure what I'm looking at. Installed the slack-build.....I'm thinking my little script as is, is all it will be..... :whistling:

 

YAD adds GUI components to scripts. I'm assuming you know the terminal commands to accomplish what you described, and are simply writing an interactive script. If it were me, I'd use YAD as a form (--form), with input boxes for video file selection and number of clips to extract, and output box for extracted clips; as well as "OK", "END" and/or possibly "REPEAT" boxes, with "OK" triggering the command line execution using the variables you input with the form. Make sense?

 

If you can write the script the way you want, I may be able to remember enough YAD to help you put a GUI around it. But it's your project, your call...

  • Like 1
Link to comment
Share on other sites

Thanks for the info.... It does sound interesting. Because I don't do this kind of stuff for a living, I don't know why something happens or what I need to do to make it happen. I know terminal commands because I enjoy working from the cli...., I sort of understand what I use. I am not familiar with the language used for writing scripts. So what I do is find an example that is close to what I want and go from there..... When I bring up YAD, I don't see much on my screen. Part of it may be because I am using spectrwm and tiling wm's may not be the best work environment. So, looking at a blank sceen with two buttons lower right saying "OK" or "Cancel" leaves me at a loss.... :'( The other thing is my small script really isn't doing very much....not sure if putting a GUI around it would add to it other than making it look nice. I'll look at it again and see if I can do something with it. If anything, it will be fun learning something new! And sure, I could use some help if I get going on it! Appreciate it. Have a good one.

Link to comment
Share on other sites

OK, looking at the man for YAD, I see dialog options....and when using them, the box now has some features other than a box with two buttons like the above screenshot.

yad2.jpg

Edited by wa4chq
Link to comment
Share on other sites

OK, looking at the links and other sites...... am sorta making sense of YAD..... doing some cut and pasting, seeing results..... also seeing that by itself like I thought in the beginning, typing yad in the cli isn't supposed to do anything other than what I see. YAD is added or used in the script. Saw this example:

action=$(yad --width 300 --entry --title "System Logout" \
   --image=gnome-shutdown \
   --button="Switch User:2" \
   --button="gtk-ok:0" --button="gtk-close:1" \
   --text "Choose action:" \
   --entry-text \
   "Power Off" "Reboot" "Suspend" "Logout")
ret=$?

BTW, this isn't the complete script....

Link to comment
Share on other sites

OK, looking at the links and other sites...... am sorta making sense of YAD..... doing some cut and pasting, seeing results..... also seeing that by itself like I thought in the beginning, typing yad in the cli isn't supposed to do anything other than what I see. YAD is added or used in the script. Saw this example:

action=$(yad --width 300 --entry --title "System Logout" \
--image=gnome-shutdown \
--button="Switch User:2" \
--button="gtk-ok:0" --button="gtk-close:1" \
--text "Choose action:" \
--entry-text \
"Power Off" "Reboot" "Suspend" "Logout")
ret=$?

BTW, this isn't the complete script....

 

You get it! Think of YAD as a bash plugin for invoking GUI elements with a script. The script is still the central piece, with YAD providing a GUI "wrapper" for the script. This is how I pictured your script variables being input. With the proper form design, all variables could be entered in a single GUI Form, rather than "echo"ing questions in series and soliciting responses.

 

Ironically, with respect to the 2nd part of your GIF creation process, I saw this earlier today on OMGubuntu:

 

http://www.omgubuntu.co.uk/2016/12/gifine-animated-gif-recorder-linux

 

Perhaps you're working too hard by creating GIFS in Gimp(?). Looks like there are several alternative GIF creator programs already existing in the wild. I'm sure there's a version for whatever you're using. (Slack?) And the Gifine software in the article needs to be compiled anyway. Just sharing info...

 

On a side note, this is why I LOVE this forum! I am a knowledge-seeker. It doesn't even have to be useful knowledge, just informative, and possibly plant the seeds of new and interesting ideas. I get that from this forum almost EVERY DAY! Sometimes I hear about things that I never new existed (such as GIF creating software?!); sometimes I get a deeper level of something that I thought I already knew, but turns out I was just somewhat familiar with. Doesn't matter to me....that's moving forward in my book, and I love that! The exchange of ideas, shared knowledge, and open-minded independent thinking on this board is invaluable. I save this forum for last in my morning info cruise. It's my dessert!

  • Like 2
Link to comment
Share on other sites

Yes it is a great forum! I've joined a few other linux forums over the years that are o.k. but BATL is the one I always go to.....great bunch of people and interesting topics, tutorials etc etc.....

Regarding GIFS....I am using GIMP because I am editing the pngs individually. Yes, just converting a png to gif wouldn't require GIMP....

  • Like 1
Link to comment
Share on other sites

I don't wanna hijack wa4chq's thread, or divert attention from helping solve his issues/questions, but I also could use some advice regarding script modification.

 

Back when I first started using Linux, and Ubuntu, one of the coolest tools I found was a script for PDF compression within the Nautilus file manager. This was a Zenity script (of which YAD was forked from, BTW) that allowed me to compress PDF files into a size that I could e-mail to clients. This was game-changing software for me that opened up a whole new and more productive workflow.

 

But it was specific to Nautilus. Long story short....when I started considering different file managers, the PDF compression script was always a deal breaker that sent me scurrying back to Nautilus. Until I stumbled upon a post discussing how to modify that script for file managers not named Nautilus. While I was interested in PCManFM, removing Nautilus-specific language allows for use in Thunar, Dolphin, etc... After making script mods, I replaced my Compress-PDF plugin in Nautilus with the modified script, which worked fine with Nautilus, as well as PCManFM. For reference, the modified script Compress-PDF 1.4, and saved as "compresspdf.sh" in /opt/madebits-pca_1.0.0 for execution as a right-click menu feature in PCManFM is as follows:

 

#! /bin/bash

# AUTHOR:   (c) Ricardo Ferreira
# NAME:	 Compress PDF 1.4
# DESCRIPTION:  A nice Nautilus script with a GUI to compress and optimize PDF files
# (adjusted to work with other file managers according to instructions here: http://askubuntu.com/a/280623/47206)
# REQUIRES: ghostscript, poppler-utils, zenity
# LICENSE:  GNU GPL v3 (http://www.gnu.org/licenses/gpl.html)
# WEBSITE:  https://launchpad.net/compress-pdf

# Messages
    # English (en-US)
    error_nofiles="No file selected."
    error_noquality="No optimization level selected."
    error_ghostscript="PDF Compress requires the ghostscript package, which is not installed. Please install it and try again."
    error_nopdf="The selected file is not a valid PDF archive."
    label_filename="Save PDF as..."
    label_level="Please choose an optimization level below."
    optimization_level="Optimization Level"
    level_default="Default"
    level_screen="Screen-view only"
    level_low="Low Quality"
    level_high="High Quality"
    level_color="High Quality (Color Preserving)"
    job_done="has been successfully compressed"

case $LANG in

#snipped to remove alternative language translations

esac

VERSION="1.4"
ZENITY=$(which zenity)

pdf_file=$(basename "$1")

# Check if Ghostscript is installed
GS="/usr/bin/ghostscript"
if [ ! -x $GS ]; then
    $ZENITY --error --title="Compress PDF "$VERSION"" --text="$error_ghostscript"
    exit 0;
fi

# Check if the user has selected any files
if [ -z "$pdf_file" ]; then
    $ZENITY --error --title="Compress PDF "$VERSION"" --text="$error_nofiles"
    exit 0;
fi

# Check if the selected file is a PDF
mimetype=$(file -b -i "$1")
if [ -z "`echo $mimetype | grep -i 'pdf' `" ]; then
   $ZENITY --error --title="Compress PDF "$VERSION"" --text="$error_nopdf"
    exit 0;
fi

# Ask the user to select a compressing format
selected_level=$($ZENITY  --list  --title="Compress PDF "$VERSION"" --text "$label_level" --radiolist  --column "" --column "$optimization_level" TRUE "$level_default" FALSE "$level_screen" FALSE "$level_low" FALSE "$level_high" FALSE "$level_color")
if [ -z "$selected_level" ]; then
    $ZENITY --error --title="Compress PDF "$VERSION"" --text="$error_noquality"
    exit 0;
fi

# Select the optimization level to use
case $selected_level in
    "$level_default")
		    COMP_COMMAND="/default"
    ;;
    "$level_screen")
		    COMP_COMMAND="/screen"
    ;;
    "$level_low")
		    COMP_COMMAND="/ebook"
    ;;
    "$level_high")
		    COMP_COMMAND="/printer"
    ;;
    "$level_color")
		    COMP_COMMAND="/prepress"
    ;;
esac

# Choose output file name
temp_filename=.temp-"$pdf_file"
suggested_filename=compressed-"$pdf_file"
output_filename=$($ZENITY --file-selection --save --confirm-overwrite --filename="$suggested_filename" --title="$label_filename")

if [ "$?" = 1 ] ; then
    exit 0;
fi

# Extract metadata from the original PDF
pdfinfo "$1" | sed -e 's/^ *//;s/ *$//;s/ \{1,\}/ /g' -e 's/^/  \//' -e '/CreationDate/,$d' -e 's/$/)/' -e 's/: / (/' > .pdfmarks
sed -i '1s/^ /[/' .pdfmarks
sed -i '/:)$/d' .pdfmarks
echo "  /DOCINFO pdfmark" >> .pdfmarks

# Execute ghostscript while showing a progress bar
(echo "0" ;
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=$COMP_COMMAND -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$temp_filename" "$1" .pdfmarks
rm .pdfmarks
echo "100") | (if `$ZENITY --progress --pulsate --auto-close --title="Compress PDF "$VERSION""`;
			 then
				 mv -f "$temp_filename" "$output_filename" &
				 notify-send "Compress PDF" "$pdf_file $job_done"
			 else
				 killall gs
				 rm "$temp_filename"
				 exit
			 fi)

 

Everything works as expected, but there is an annoying behavior (feature?!) that I'd like to tweak. I'd like to output the compressed file to the SAME DIRECTORY as the input file, by DEFAULT, unless I select otherwise. As it is, the script defaults to the /home directory, and I have to "drill down" each time in order to arrive at the appropriate sub-directory. Often, I am multiple layers deep, and this is a huge PITA. Is there a way to further modify this script to automatically default to the directory the input file is in?

 

For instance...if I want to compress a 15GB Report.pdf located in /home/docs/word/summary&appraisal/2016 I must currently:

1 - go to file location in PCManFM

2 - right click file

3 - choose CompressPDF option

4 - select appropriate compression options (default work fine 99% of the time)

5 - navigate from /home directory through multiple layers of directories, to /home/docs/word/summary&appraisal/2016

6 - select "Report.pdf" in order to change default filename from "compressed-Report.PDF" (I'm okay with this behavior, it's a fail-safe check for me...)

7 - save, and select "overwrite Report.pdf"

 

step #5 is the problem. I almost always save to the same directory, overwriting the original file, and I could eliminate 5-6 additional "click selections" if I could get the script to default to the same working directory that the input file is in. Any of you Bash experts/script gurus have a suggestion on how to modify this script to accomplish that?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...