Damn Small Linux (DSL) Forums

MyDSL Extensions => Extension Development => Topic started by: carlo on April 17, 2014, 01:45:50 PM

Title: synchronal stopwatch
Post by: carlo on April 17, 2014, 01:45:50 PM

 When I'm doing video stuff, I have found out that a combination of Mplayer with a stopwatch works fine to make sound and image going synchronal later on in Avidemux and Audacity. As a result of my low cpu (=666MHz), Avidemux isn't synchronal.
I managed to install a stopwatch from Debian Etch. The only problem now is that I can't launch it (started) together with Mplayer . I always have to start the stopwatch manually after appearance, which is tricky.
So I was wandering if someone knows about a stopwatch install-able in DSL, that can be launched "started" at appearance?
I Thought that maybe there is a possibility with Xmacro. (Recording mouse and keyboard events). But when I run Xmacrorec my mouse dont work anymore. To make a script for Xmacroplay, I haven't enough knowledge yet.


Title: Re: synchronal stopwatch
Post by: CNK on April 17, 2014, 11:22:15 PM
It's a bit hard to work out what you're doing, but if you just want to start both programs at the same time, a script should do that for you. There are probably better ways, but I'll deccribe one method:

Open a terminal window and type "nano", then press enter. Type "gmplayer | <Name of Stopwatch Program>". Press ctrl-X, say yes you want to save and give it a file name like "videoedit.sh".

Then when back at the command prompt, give the following command:

sudo chmod a+rwx videoedit.sh

Then to test, type "./videoedit.sh" and both programs should start. You can add a program icon to the desktop by typing the full path on the script into the "create new program icon" window. Probably "/home/dsl/videoedit.sh".

Title: Re: synchronal stopwatch
Post by: carlo on April 18, 2014, 08:12:52 AM
Thanks for the information but,

every stopwatch has a start-button.
I want that button to be activated at appearance from the command line if possible. So that I can do "mplayer myfile.avi | stopwatch (-?)".
It's as if I want to launch Audacity with "audacity myfile.mp3 -start", and it already runs when it appears, which it doesn't by default.


Title: Re: synchronal stopwatch
Post by: CNK on April 18, 2014, 10:48:52 PM
Doesn't look like that will work with the Debian stopwatch program. Try searching Google for "terminal stopwatch Linux". The second result could have potential.

Title: Re: synchronal stopwatch
Post by: carlo on April 21, 2014, 08:42:49 AM
total=0;nl=$'\n';key=go; while [[ $key != 's' ]]; do result=$( (time read -sn1; echo "$REPLY") 2>&1 );result=${result/$nl/ };key=${result#* };lap=${result% *}; thou=${lap#0};thou=${thou/.};let total+=$thou;echo "Lap $lap"; done; echo "Total ${total:0:${#total} - 3}.${total: -3:3}"

This one looks interesting to me. I had managed to have those lap-times addition-ed instead of separated. But when it begins to count more than 1 minute it keeps on counting only seconds. For that the next one could be better, but that one can't be started together with mplayer. I thought I'll mix the 2 watches together, but it seems to be in different languages. I'm a new-by in scripting. I also noticed that when I start mplayer, in "the terminal" it gives the elapsed seconds (like the first stopwatch) , so if I could put that in an order of "minutes, seconds, milliseconds",...      But where? I have been searching, and nothing appeared.


# A command line Stopwatch

# License: LGPLv2
# Author:
#    http://www.pixelbeat.org/
# Notes:
#    This script starts a few processes per lap, in addition to
#    the shell loop processing, so the assumption is made that
#    this takes an insignificant amount of time compared to
#    the response time of humans (~.1s) (or the keyboard
#    interrupt rate (~.05s)).
#    '?' for splits must be entered twice if characters
#    (erroneously) entered before it (on the same line).
#    '?' since not generating a signal may be slightly delayed
#    on heavily loaded systems.
#    Lap timings on ubuntu may be slightly delayed due to:
#    https://bugs.launchpad.net/bugs/62511
# Changes:
#    V1.0, 23 Aug 2005, Initial release
#    V1.1, 26 Jul 2007, Allow both splits and laps from single invocation.
#                       Only start timer after a key is pressed.
#                       Indicate lap number
#                       Cache programs at startup so there is less error
#                       due to startup delays.
#    V1.2, 01 Aug 2007, Work around `date` commands that don't have nanoseconds.
#                       Use stty to change interrupt keys to space for laps etc.
#                       Ignore other input as it causes problems.
#    V1.3, 01 Aug 2007, Testing release.
#    V1.4, 02 Aug 2007, Various tweaks to get working under ubuntu and Mac OS X.
#    V1.5, 27 Jun 2008, set LANG=C as got vague bug report about it.

export LANG=C

ulimit -c 0   #no cores from SIGQUIT
trap '' TSTP  #ignore Ctrl-Z just in case
save_tty=`stty -g` && trap "stty $save_tty" EXIT #restore tty on exit
stty quit ' ' #space for laps rather than Ctrl-\
stty eof  '?' #? for splits rather than Ctrl-D
stty -echo    #don't echo input

cache_progs() {
    stty > /dev/null
    date > /dev/null
    grep . < /dev/null
    (echo "import time" | python) 2> /dev/null
    bc < /dev/null
    sed '' < /dev/null
    printf '1' > /dev/null
    /usr/bin/time false 2> /dev/null
    cat < /dev/null
cache_progs #to minimise startup delay

date +%s.%N | grep -qF 'N' && use_python=1 #if `date` doesn't have nanoseconds
now() {
    if [ "$use_python" ]; then
        echo "import time; print time.time()" 2>/dev/null | python
        printf "%.2f" `date +%s.%N`

fmt_seconds() {
    mins=`echo $seconds/60 | bc`
    if [ "$mins" != "0" ]; then
        seconds=`echo "$seconds - ($mins*60)" | bc`
        echo "$mins:$seconds"
        echo "$seconds"

total() {
    total=`echo "$end - $start" | bc`
    fmt_seconds $total

stop() {
    [ "$lapped" ] && lap "$laptime" "display"

lap() {
    laptime=`echo "$1" | sed -n 's/.*real[^0-9.]*\(.*\)/\1/p'`
    [ ! "$laptime" -o "$laptime" = "0.00" ] && return #signals too frequent
    laptotal=`echo $laptime+0$laptotal | bc`
    if [ "$2" = "display" ]; then
        lapcount=`echo 0$lapcount+1 | bc`
        laptime=`fmt_seconds $laptotal`
        echo $laptime "($lapcount)"

echo -n "Space for lap | ? for split | Ctrl-C to stop | Space to start...">&2

while true; do
    trap true INT QUIT #set signal handlers
    laptime=`/usr/bin/time -p 2>&1 cat >/dev/null`
    trap '' INT QUIT #ignore signals within this script
    if [ $ret -eq 1 -o $ret -eq 2 -o $ret -eq 130 ]; then #SIGINT = stop
        [ ! "$start" ] && { echo >&2; exit; }
    elif [ $ret -eq 3 -o $ret -eq 131 ]; then #SIGQUIT = lap
        if [ ! "$start" ]; then
            start=`now` || exit 1
            echo >&2
        lap "$laptime" "display"
    else #eof = split
        [ ! "$start" ] && continue
        lap "$laptime" #update laptotal

Title: Re: synchronal stopwatch
Post by: CNK on April 21, 2014, 10:39:18 PM
I haven't tried it because I don't want to install python and bc on the computer I'm working on (required by the second script), but
            start=`now` || exit 1
            echo >&2
At about 10 lines from the bottom looks like the command to begin. I still need to get around to learning Linux scripting though, so I can't tell you much more than trying to put this in at the start of the script looks like something to try.

The first script would also be easy to modify by simply putting in a bit to query if the seconds counter is equal to 60, and if so add one to the minutes counter and reset seconds to zero - I just don't know the syntax to do this in a script. By the way, the first script looks different because it is mostly done in one line, commands separated by a semicolon.

I'm a bit confused by why you ask about the mplayer time. I never worked out why you can't just use the time display in gmplayer. If you need the terminal program, you can bring up an on-screen time display by pressing the "o" key a couple of times.

Title: Re: synchronal stopwatch
Post by: carlo on April 22, 2014, 06:56:23 AM
I need time displayed in, at least hundredth of seconds to adjust sound-delay in Audacity.

Title: Re: synchronal stopwatch
Post by: CNK on April 22, 2014, 10:35:29 PM
Shouldn't that delay be the same on each video?

What are you actually trying to do to the video?

Perhaps VirtualDub running with Wine, or ffmpeg could do the job better?

Title: Re: synchronal stopwatch
Post by: carlo on April 25, 2014, 11:19:45 AM
When I have a video file compound of several other files, in which I had to change volume, I must synchronize every sound file separately, so I have to know the right place to begin each take.
What do you think I can do with ffmpeg?

Title: Re: synchronal stopwatch
Post by: CNK on April 25, 2014, 11:11:31 PM
OK, that makes more sense. As you mentioned the speed of your PC, I thought Avidemux might have been having trouble keeping the audio and video in sync at some stage. I suggested the other programs because some jobs could be done with them instead (cutting video, changing video specs, etc.).

Actually I'm not sure that a stopwatch would be the best method as any delay in the video wouldn't be accounted for so you'd have to rely on the video playback working perfectly (or at least being able to keep up with lag by dropping frames). VirtualDub might give you the video time in miliseconds, I'd have to check.

Might be possilbe to make a script to operate on Mplayer play time printed by the terminal program, I just don't know how.

Powered by SMF 1.1.20 | SMF © 2013, Simple Machines