LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-22-2014, 05:25 PM   #1
rrrssssss
Member
 
Registered: Mar 2005
Posts: 288

Rep: Reputation: 34
Zenity to record movie using "cat" command


Hello Gentoo networking forum,

I have a Hauppauge HD-PVR USB capture box for recording high definition movies (from a Dish Receiver) and whenever I want to record a movie I just type "cat /dev/video0 > name-of-movie.ts" in a terminal window and the movie will start recording into my home folder (.ts extension). I then can play it back with a Linux media player (mplayer) with excellent quality.

I would like to know how to configure Zenity so that I can execute it and be greeted with a dialogue box that prompts me to type the name of the movie in the field, then press OK and the movie starts recording using the "cat" command mentioned above.

I can already do something similar with the script shown below (not using Zenity) but after the movie is finished I have to rename the movie to the name of the movie since the script does not know what the name of the movie is. The name of the movie is always "temp". But using a Zenity configuration, I can type the movie name in the dialogue box's field before it starts recording. Also, the script below will start a media player a few seconds after the movie starts recording so the movie can be viewed as it is being recorded. To stop the recording and kill everything I just close the media player's window. It is all included in the script below. By the way, I did not write the script below, I found it on the Internet years ago but it is a very nice script. I would like to incorporate the script below into Zenity if it is possible.

I know I am asking a lot from you programmers, but all of the excerpts in the script below do not necessarily need to be included in the Zenity configuration but it would nice to have them included.

Code:
	

#!/bin/bash

#http://ubuntuforums.org/archive/index.php/t-254127.html
# Some variables to tweak
set -x

#Below commands prevent root access
if [ "$UID" = "0" ]
then echo "Tut tut, you should know better than that!"; exit 1;
fi

v4l2-ctl -d /dev/video0 -i0
v4l2-ctl --set-audio-input=0

TEMP_NAME="/home/royroy/temp"
VIDEO_DEV="/dev/video0"
VIDEO_PLAYER="/usr/bin/mplayer2 -geometry 960x540    -aspect 16:9"

# Find a free filename
i="1"
while [ -e "$TEMP_NAME$i.ts" ]
do
i=$[$i+1]
done
VIDEO_NAME="$TEMP_NAME$i.ts"

# Capture video in background
cat $VIDEO_DEV > "$VIDEO_NAME" &
CHILD_PID=$!

# Wait 5 seconds
sleep 5

# Is the video still buffering?
if [ -z `pgrep -P $$` ]
then echo "Oh hell... something went all pear-shaped :("; exit 1;
fi

# Play the video
$VIDEO_PLAYER "$VIDEO_NAME"

# We're back - Clean up now!
#kill $CHILD_PID
#wait

# How much 'cleaning' should we do?
# if [ "$1" != "-k" ]
# then rm -f "$VIDEO_NAME"
# else echo "What you've just seen is at $VIDEO_NAME"
# fi

set +x
Sincerely and thanks in advance,
Roy

Last edited by rrrssssss; 05-22-2014 at 05:28 PM.
 
Old 05-22-2014, 05:41 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
How about just:
Code:
VIDEO_NAME=$(zenity --entry --text='Enter a name for the movie')
BTW - variables in all caps are generally reserved for environment variables. Local script variables should generally be in lower case or mixed lower/upper case for readability.

Last edited by suicidaleggroll; 05-22-2014 at 05:43 PM.
 
Old 05-22-2014, 06:17 PM   #3
rrrssssss
Member
 
Registered: Mar 2005
Posts: 288

Original Poster
Rep: Reputation: 34
Code:
VIDEO_NAME=$(zenity --entry --text='Enter a name for the movie')
I really appreciate your help, suicidaleggroll. It worked! I just had to add "$i.ts" at the end of the above line to automatically give the movie name a .ts extension. Otherwise I would have to manually type the movie extension (.ts) at the end of the movie name I put in the Zenity box.

So now the line looks like this:

Code:
VIDEO_NAME=$(zenity --entry --text='Enter a name for the movie')$i.ts

Again, I really appreciate you helping me.

Now I've got to figure out why the movie goes on my desktop instead of my home folder like the script tells it to (TEMP_NAME="/home/roy/temp"). The movie will be saved in whatever folder the script is in so if the script is in the Desktop folder, the video will go there also. Before we put Zenity in the script, the command TEMP_NAME="/home/roy/temp" would be honored. But not with Zenity for some reason.

All the best,
Roy

Last edited by rrrssssss; 05-22-2014 at 07:00 PM.
 
Old 05-22-2014, 08:07 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
The $i is to add a number to the end in case the original file already exists. Do you want that? If not, you can simplify the script a bit:
Code:
#!/bin/bash

#http://ubuntuforums.org/archive/index.php/t-254127.html
# Some variables to tweak
set -x

#Below commands prevent root access
if [ "$UID" = "0" ]
   then echo "Tut tut, you should know better than that!"; exit 1;
fi

v4l2-ctl -d /dev/video0 -i0
v4l2-ctl --set-audio-input=0

DIR="/home/royroy/"
VIDEO_DEV="/dev/video0"
VIDEO_PLAYER="/usr/bin/mplayer2 -geometry 960x540    -aspect 16:9"

# prompt the user for a file name
NAME=$(zenity --entry --text='Enter a name for the movie')
exitstat=$?
if [[ -z "$NAME" || $exitstat -ne 0 ]]; then
   echo "Cancelled"
   exit
fi
VIDEO_NAME=${DIR}/${NAME}.ts

# Capture video in background
cat $VIDEO_DEV > "$VIDEO_NAME" &
CHILD_PID=$!

# Wait 5 seconds
sleep 5

# Is the video still buffering?
if [ -z `pgrep -P $$` ]
   then echo "Oh hell... something went all pear-shaped :("; exit 1;
fi

# Play the video
$VIDEO_PLAYER "$VIDEO_NAME"

# We're back - Clean up now!
#kill $CHILD_PID
#wait
If you don't want the script to exit if the user enters a blank name or clicks "Cancel", then you can modify that if statement appropriately.

Last edited by suicidaleggroll; 05-22-2014 at 08:12 PM.
 
Old 05-23-2014, 04:33 AM   #5
rrrssssss
Member
 
Registered: Mar 2005
Posts: 288

Original Poster
Rep: Reputation: 34
I thank you once again for helping me suicidaleggroll.

Code:
# prompt the user for a file name
NAME=$(zenity --entry --text='Enter a name for the movie')
exitstat=$?
if [[ -z "$NAME" || $exitstat -ne 0 ]]; then
   echo "Cancelled"
   exit
fi
You are correct in saying I do not need (or want) the number "1" to be added to files of the same name (temp) since I am typing a unique name to start with. But using the new script you suppled caused the output file to be an unplayable text file so I made this sleight modification:

Code:
# prompt the user for a file name
NAME=$(zenity --entry --text='Enter a name for the movie')
exitstatus=$?
if [[ -z "$NAME" || $exitstat -ne 0 ]]; then
   echo "Cancelled"
   exit
fi
I changed "stat" to "status". Now it works perfectly. And the new script will now honor this line:

Code:
DIR="/home/roy/"
meaning I can change what directory the movie will be saved it, unlike the way it was before.


And I had to uncomment this line:

Code:
# We're back - Clean up now!
kill $CHILD_PID
#wait

To me you are a genius.

Thank you again,
Roy

PS

FYI,

The bottom section in the original script (before modification) was commented out, but the purpose of it was so you could watch "live tv" without saving the file, meaning when the script was executed, you could watch what was being recorded and once you closed the player, the script would delete the recording. I mean, nobody would necessarily want to save the recording anyway if all they wanted to do was watch live tv. But once the bottom section was commented out, the movie file would remain for thise who wanted to keep the recording. I have always been impressed with this script.

Code:
# How much 'cleaning' should we do?
 if [ "$1" != "-k" ]
 then rm -f "$VIDEO_NAME"
 else echo "What you've just seen is at $VIDEO_NAME"
 fi

Last edited by rrrssssss; 05-23-2014 at 06:06 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
is "cat" command limited to only 2 files ? robgrune Linux - Newbie 13 04-11-2014 10:44 AM
[SOLVED] Why "cat" command is not executed as a separate process ? techie_san778 Linux - Server 4 10-19-2013 12:55 AM
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
[SOLVED] Need an explanation of here scripts as well as the "cat" command UNGR Linux - Newbie 11 08-13-2011 10:08 AM
Feeding the output of "diff" or "cat" command to dpkg --purge kushalkoolwal Debian 9 06-19-2008 07:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration