The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script find files in two dirs HELP fierusbentus Shell Programming and Scripting 2 03-03-2009 08:27 AM
[Perl] Accessing array elements within a sed command in Perl script userix Shell Programming and Scripting 2 10-03-2008 12:05 PM
how can i pass parameter with spaces to csh script umen Shell Programming and Scripting 1 03-19-2008 12:33 PM
shell script to create dirs sjajimi Shell Programming and Scripting 2 07-24-2007 07:34 AM
Strip leading and trailing spaces only in a shell variable with embedded spaces jerardfjay Shell Programming and Scripting 6 03-07-2005 02:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-16-2009
audiophile audiophile is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 27
Thumbs down perl script chokes on dirs with spaces

Basically, I have a perl script that calls enables one to resume where a video file left off (i.e. where the user stopped playback) that chokes on files that are located in a path that contains spaces.

To make the situation a little more complicated, the perl script gets help from a bash script that analyzes the video file in question and passes file-specific switches to mplayer.

Both scripts need to be in the same dir and you must run them from that dir!

Here is the usage:
Code:
$ ./mplayer-resumer-vdpau.pl /path/to/file.avi
(You can use any video format that mplayer can play, like mpg, mkv, etc.)

The problem occurs when the path to the video file contains a space. The perl script doesn't handle it and passes the incorrect options to the file.

Example: I go to play ‘Test.mkv’ located in /media/D/New\ CD/

Code:
$ ./mplayer-resumer-vdpau.pl /media/D/New\ CD/Test.mkv 
File not found: '/media/D/New'
Failed to open /media/D/New.
File not found: '/media/D/New'
Failed to open /media/D/New.
Can someone have a look at the code and help me fix this? I just don't know that much about perl to be useful.

Code to mplayer-resumer-vdpau.pl
Code:
#!/usr/bin/perl

use Shell;
use strict;
use POSIX qw(floor);

# Written by Bob Igo from the MythTV Store at http://MythiC.TV
# Email: bob@stormlogic.com
#
# If you run into problems with this script, please send me email

# CHANGE LOG:
# 3/9/2009
# Modded Bob's wonderful resumer script to work with the vdpau
# options detector script by nickca of the linhes forums.
#
#  http://www.knoppmythwiki.org/index.php?page=HardwareAcceleratedVideo
#
# You need to have both this and the vdpau-detector script in the same
# dir for this to work properly.  Also, change your default mplayer
# options in mythfrontend to read simply: # mplayer-resumer-vdpau.pl %s
#
# To tweak the shell commands for mplayer, edit the vdpau-detector script.
# It has a small README inside it as comments
# ENJOY!

# 5/3/2006
# Added last time started checking.
# If this script is restarted within $tdiff (default 5 seconds)
# then it will delete the file used to keep track of the videos
# resume position.

# PURPOSE:
# --------------------------
# This is a wrapper script to prove the concept of having MythTV
# resume playback of previously-stopped video where you left off.
# It's likely that a good solution will look different than this
# initial code.

# RATIONALE:
# --------------------------
# Watching 90% of a video and stopping causes you to return to the
# beginning again the next time you watch it.  Remembering where
# you were in the video and resuming at that position is a much nicer
# behavior for the user.
#
# By default, mplayer spits out timecode information that tells you
# where you are in the video, to the tenth of a second.  Mplayer also
# supports a seek feature on the command-line.  We can make use of these
# features to write an mplayer wrapper that will remember the last
# position in a video file and resume to it on playback.

# PARAMETERS:
# --------------------------
# see print_usage() below

# FILES:
# --------------------------
# $infile, the video to play
# $resumefile, the video's resume file (see get_resume_filename() below)

# KNOWN ISSUES:
# --------------------------
# Mplayer misreports the timecodes on .nuv MPEG-2 files.  Currently, anything
# captured via an HDTV tuner card and put into your /myth/video directory
# will fail with this resumer.
#
# Current theories include the timecode having to do with the show's broadcast
# time, recording time, or perhaps its upload time to the station that
# broadcast it.

# DESIGN LIMITATION:
# -------------------------
# If the video file to be played is on a read-only filesystem, or otherwise
# lives in a location that cannot be written to, resume will fail.  This is
# because the current implementation uses a file parallel to the video file
# to store the timecode.
#

my $infile;
my $resumefile;
my $mplayer_parameters;
my $fudge_factor=2; # number of additional seconds to skip back before playback
my $tnow;    # Time now.
my $tprev;   # Time the prog was last started. 
             # Returned from the modification time of the xx.resume file.
my $tdiff=5; # How many seconds before we should start from 
             # the beginning of the movie
#DEBUG
#open(DEBUG,">/tmp/debug") || die "unable to open debug file";

sub init () {
    $tnow = time();
    $infile = @ARGV[$#ARGV];

    $resumefile = &get_resume_filename($infile);
    # This returns the 9th element of the 13 element array 
    # created by the stat function.
    $tprev = (stat ($resumefile))[9]; 
    # if this file is restarted in less than 5 seconds then 
    # remove the .resume file
    if ( ($tnow - $tprev) < $tdiff ) {
        unlink($resumefile);
    }

    $mplayer_parameters = join(' ',@ARGV[0..$#ARGV-1]);
}

&init();
&save_time_offset(&mplayer($mplayer_parameters,$infile), $resumefile);

#close(DEBUG);

# For $pathname, return $path.$filename.resume
sub get_resume_filename () {
    my($pathname)=@_;
    
    my $idx = rindex($pathname,"/");

    if ($idx == -1) { # There was no "/" in $pathname
    return ".".$pathname.".resume";
    } else {
    # Now we need to split out the path from the filename.
    my $path = substr($pathname,0,$idx+1);
    my $filename = substr($pathname,$idx+1);
    return "$path.$filename.resume";
    }
}

# Calls mplayer and returns the last known video position
sub mplayer () {
    my($parameters,$infile)=@_;
    my $seconds=0;
    my $timecode=&get_time_offset($infile);
    my $mplayeropts=`./vdpau-detector $infile`; chomp $mplayeropts;
    my @lines = split "\n", $mplayeropts;
    my $last_line = pop(@lines);
    my $command = "mplayer $last_line $parameters -ss $timecode \"$infile\" 2>&1 2>/dev/null |";

    open(SHELL, $command);
    # The kind of line we care about looks like this example:
    # A:1215.2 V:1215.2 A-V:  0.006 ct:  0.210 207/201 13%  0%  1.9% 0 0 68%
    # But all we care to look at is the first number.
    
    while (<SHELL>) {
    #print DEBUG $_;
    if (m/A: *[0-9]+\.[0-9]/) { # See if this line has timecodes on it
        my $last_timecode_line = &extract_last_timecode_line($_);
        if ($last_timecode_line =~ m/ *([0-9]+\.[0-9]) V/) {
        $seconds=$1;
        }
    }
    }
    close(SHELL);

    return $seconds;

    sub extract_last_timecode_line () {
    my ($line)=@_;
    my @lines=split('A:',$line);
    return @lines[$#lines-1];
    }
}

# Save the last known video position
sub save_time_offset () {
    my($seconds, $resumefile)=@_;

    open(RESUMEFILE, ">$resumefile") || die "Unable to open $resumefile for writing";
    print RESUMEFILE "$seconds";
    close(RESUMEFILE);
}

# returns the number of seconds corresponding to the last known video position,
# in hh:mm:ss format, compatible with the "-ss" parameter to mplayer
sub get_time_offset () {
    my($videofile)=@_;
    my($resumefile) = &get_resume_filename($videofile);
    my $seconds=0;
    my $timecode;

    open(RESUMEFILE, "<$resumefile") || return "00:00:00";
    while(<RESUMEFILE>) {
    $seconds=$_;
    }
    close(RESUMEFILE);

    my $hours = floor($seconds/3600);
    $seconds = $seconds - $hours*3600;

    my $minutes = floor($seconds/60);
    $seconds = int($seconds - $minutes*60) - $fudge_factor;

    $timecode = sprintf "%02d:%02d:%02d",$hours,$minutes,$seconds;
#    print "TIMECODE: $timecode\n";
    return $timecode;
}

sub print_usage () {
    print "USAGE:\n";
    print "\t",$ARGV[0], "[mplayer parameters] video_file\n";
    print "\t","e.g. ",$ARGV[0], "-fs -zoom my.mpg\n";
    print "\t","Version 3/9/2009\n";
}
Code to vdpau-detector
Code:
#!/bin/bash

# This script written by nickca on the linhes forums
# Original page http://www.knoppmythwiki.org/index.php?page=HardwareAcceleratedVideo
#
# It's modded to allow vdpau switches commands to pass to a modded version of Bob Igo's
# mplayer-resumer.pl wrapper for mplayer
#
# User can customize commandline for mplayer by editing the appropriate sections below
# They include BASIC_OPTS, NOHD_OPTS, and LOW_OPTS

IAM=$0
FILE=$1

#if no input display usage
if [[ -z "$FILE" ]]; then
        echo usage: $0 /path/to/file
        exit
fi

MPLAYER="/usr/bin/mplayer"

# For a complete list of options, see the mplayer DOCS
# http://www.mplayerhq.hu/DOCS/man/en/mplayer.1.html
#

# Options for all videos: fullscreen, zoom, and use software eq
BASIC_OPTS="-fs -zoom"

# Options for non-HD videos: force aspect to 16x10 (to ensure video fills TV screen)
# You'll need to modify the ratio if your res differs
# Example: 1920x1080 = 1.778 = 16:9
# Example: 1680x1050 = 1.600 = 16:10
# Example: 1280x720 = 1.778 = 16:9

NOHD_OPTS="-aspect 16:10"

# Options for normal and lowres videos: use XV forcing 16x10
LOW_OPTS="-aspect 16:10 -vo xv"

VWIDTH=$($MPLAYER -identify -vo vdpau -frames 0 "$FILE" | grep ID_VIDEO_WIDTH | cut -c 16-25)
VCODEC=$($MPLAYER -identify -vo vdpau -frames 0 "$FILE" | grep ID_VIDEO_CODEC | cut -c 16-25)

# all following echo commands can be uncommented for debug info
#echo .
#echo " DEBUG info: video codec: $VCODEC"
#echo " DEBUG info: video width: $VWIDTH"
#echo " Debug info: 


# Test for codec, if it's supported by VDPAU, set options to use it. Then, check if the video is in HD,
# and if it's not, set non-HD options.
case $VCODEC in
ffh264) 
        #echo -e "Playing h.264 file $FILE:\n"
        MPLAYEROPTS="$BASIC_OPTS -vo vdpau -vc ffh264vdpau"
        if [ $VWIDTH -lt 1280 ] && [ $VWIDTH != 0 ]; then
                MPLAYEROPTS="$MPLAYEROPTS $NOHD_OPTS"
        fi
        if [ $VWIDTH -lt 700 ] && [ $VWIDTH != 0 ]; then
                MPLAYEROPTS="$BASIC_OPTS $LOW_OPTS"
        fi
        ;;
ffmpeg2)
        #echo -e "Playing MPEG2 file $FILE:\n"
        MPLAYEROPTS="$BASIC_OPTS -vo vdpau -vc ffmpeg12vdpau"
        if [ $VWIDTH -lt 1280 ] && [ $VWIDTH != 0 ]; then
                MPLAYEROPTS="$MPLAYEROPTS $NOHD_OPTS"
        fi
        if [ $VWIDTH -lt 700 ] && [ $VWIDTH != 0 ]; then
                MPLAYEROPTS="$BASIC_OPTS $LOW_OPTS"
        fi
        ;;
ffwmv3)
        #echo -e "Playing WMV3 file $FILE:\n"
        MPLAYEROPTS="$BASIC_OPTS -vo vdpau -vc ffwmv3vdpau"
        if [ $VWIDTH -lt 1280 ] && [ $VWIDTH != 0 ]; then
                MPLAYEROPTS="$MPLAYEROPTS $NOHD_OPTS"
        fi
        if [ $VWIDTH -lt 700 ] && [ $VWIDTH != 0 ]; then
                MPLAYEROPTS="$BASIC_OPTS $LOW_OPTS"
        fi
        ;;
# VC-1 is largely unsupported by nvidia - uncomment this section if you're sure your card supports it.
ffvc1)
        #echo -e "Playing VC-1 file $FILE:\n"
        MPLAYEROPTS="$BASIC_OPTS -vo vdpau -vc ffvc1vdpau"
        if [ $VWIDTH -lt 1280 ] && [ $VWIDTH != 0 ]; then
                MPLAYEROPTS="$MPLAYEROPTS $NOHD_OPTS"
        fi
        if [ $VWIDTH -lt 700 ] && [ $VWIDTH != 0 ]; then
                MPLAYEROPTS="$BASIC_OPTS $LOW_OPTS"
        fi
        ;;
*)
        #echo -e "Playing normal file $FILE:\n"
        # Use XV and yadif filter with 'normal' (DiVX, XViD, old WMV, etc.) files, and force 16:9
        # -vf filters only seem to work with XV, or at least they don't work w/VDPAU
        MPLAYEROPTS="$BASIC_OPTS $LOW_OPTS"
        ;;
esac

echo $MPLAYEROPTS
  #2 (permalink)  
Old 03-16-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
can't you quote the path with spaces?

$ ./mplayer-resumer-vdpau.pl '/media/D/New CD/Test.mkv'
  #3 (permalink)  
Old 03-16-2009
audiophile audiophile is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 27
It gave the same error... it must be something in how the perl script is calling the bash script because the bash script works just fine in dirs with and without spaces....
  #4 (permalink)  
Old 03-16-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
did you try double-quotes?

$ ./mplayer-resumer-vdpau.pl "/media/D/New CD/Test.mkv"

that should work on Windows
  #5 (permalink)  
Old 03-16-2009
audiophile audiophile is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 27
Yeah, single or double quotes... I'm sure it's something in the perl code
  #6 (permalink)  
Old 03-16-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
assuming this is the line in the perl code that is problematic:

my $mplayeropts=`./vdpau-detector $infile`; chomp $mplayeropts;

try putting quotes around $infile (assuming that is the variable with the space):

my $mplayeropts=`./vdpau-detector \"$infile\"`; chomp $mplayeropts;

you can experiment with the type of quotes or adding an escape before the quotes: \"
  #7 (permalink)  
Old 03-17-2009
audiophile audiophile is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 27
Thanks for the suggestion, but no go
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0