VLC bash transcoding script destination directory

0

Well, I have such transcoding script:

#!/bin/bash
acodec="vorb"  
arate="256" 
ext="ogg" 
vlc="/usr/bin/vlc" 
fmt="mp3" 

for a in *$fmt; do 
$vlc -I dummy -vvv "$a" --sout "#transcode{acodec=$acodec,ab=$arate,channels=2}:duplicate{dst=std{access=file,mux=ogg,dst=\"${PWD##*/}/${a%%.*}.$ext\"}" vlc://quit 
done

But I want to place output files in other separate directory with the same name as source. For example, if I convert all files from folder

/usr/tmp/pop_music

I want to place converted files in directory

/$user/pop_music

I've made all possible variations and always get error. With the current (above) variant I get error:

[0xb500abe0] main stream out debug: destroying chain... (name=(null))
[0xb500abe0] main stream out debug: destroying chain done [0xb5000588] main stream output error: stream chain failed for `transcode{acodec=vorb,ab=256,channels=2}:duplicate{dst=std{access=file,mux=ogg,dst="1/Alabama - If You're Gonna Play in Texas (You Gotta Have a Fiddle in the Band).ogg"}'
[0x95be4f8] main input error: cannot start stream output instance, aborting
[0x96985a8] main playlist debug: finished input
[0x96985a8] main playlist debug: dead input

I tried to specify in dst:
1) 1/XXX.ogg
2) /1/XXX.ogg
3) 1\XXX.ogg
4) \1\XXX.ogg
but with no luck...

Suncatcher

Posted 2014-03-12T18:37:00.107

Reputation: 908

Does the target directory exist? VLC will probably not automatically create it for you. – Daniel Andersson – 2014-03-12T18:53:57.847

Answers

0

If the output directory does not exist, VLC fails with that exact message.

This is a small expansion of the script. In the current state it takes directories as arguments and loops through them. If no argument is given, it looks for files in the current directory. Output files are written to the home directory, under a subdirectory named as the parent directory of each given file. The output directories are created upon invocation to avoid the error message.

#!/bin/sh
usage() {
    cat <<-USAGE 1>&2
    usage: ${0##*/} [DIR ...]

    optional arguments:
      DIR   directories to search. Use current directory if none are given.
    USAGE
    exit 1
}

error() {
    printf '%s: error: %s, aborting.\n' "${0##*/}" "$1" 1>&2
    exit $2
}

transcode_dir() {
    indir=$(readlink -f -- "$1")
    outdir="$HOME/${indir##*/}"

    ! mkdir -pv -- "$outdir" && error "cannot create directory \"$outdir\"" 3

    cd -- "$indir"
    for a in *.$fmt; do
        $vlc -I dummy -vvv "$a" --sout "#transcode{acodec=$acodec,ab=$arate,channels=2}:duplicate{dst=std{access=file,mux=ogg,dst=\"$outdir/${a%.*}.$ext\"}}" vlc://quit
    done
    cd - 2> /dev/null
}

while getopts "h" option; do
    case $option in
        h|\? )  usage;;
    esac
done
shift $((OPTIND-1))

acodec="vorb"
arate="256"
ext="ogg"
vlc="/usr/bin/vlc"
fmt="mp3"

if [ $# -eq 0 ]; then
    transcode_dir .
else
    while [ $# -ge 1 ]; do
        [ ! -d "$1" ] && error "not a directory: \"$1\"" 2
        transcode_dir "$1"
        shift
    done
fi

A lot of lines, but most of it is just pasted boiler-plate code (feel free to ask if any part is unclear). I changed the interpreter to #!/bin/sh since no Bash specific constructs are used. I moved the transcoding part to a function for clarity.

Daniel Andersson

Posted 2014-03-12T18:37:00.107

Reputation: 20 465

Thanks for your help. Does this usual behavior (directories should be created manually) for UNIX-based systems? Or it is VLC specifics? In Windows if destination is specified via slash the directory will be created automatically I know – Suncatcher – 2014-03-13T07:13:14.643

@Dezmond: I think it is quite common for programs to fail if the path is not available. At least they should ask something like "The directory X does not exist, should it be created?" before doing it. That is the way e.g. Windows installers generally solve it; exactly how it is solved on a program to program basis varies. – Daniel Andersson – 2014-03-13T07:38:28.957

Tried your script and it wrote: "Syntax error: end of file unexpected (expecting "}")" – Suncatcher – 2014-03-23T16:47:59.230

@Dezmond: I tried pasting and running it locally, and it worked. Check your pasting again. I'm running it with Dash as interpreter, which quickly breaks on anything non-standard, so I can't currently see where the syntax error could come from if it is pasted correctly. Note though that the script gives unexpected results if there are no matching files in the given directory, but that should make itself clear upon trying. So, write the script to e.g. the file transcoder.sh, make it executable: chmod +x transocer.sh and run it with ./transcoder.sh. – Daniel Andersson – 2014-03-23T17:04:46.340

"No matching files" and "syntax error" are different situations:) Okay, I'll try again soon – Suncatcher – 2014-03-24T05:22:43.680

The script definitely does NOT work. I tried both with directory as argument and without. The script was in the folder with the 3 mp3-files and gave the same error. – Suncatcher – 2014-03-30T20:03:11.873

And what do you mean by "locally"? Script can be run via network?)) – Suncatcher – 2014-03-30T20:04:02.563

@Dezmond: "Locally" as in "on my computer". Try grabbing the script from http://pastebin.com/DhZ7yjsC instead; perhaps there is some issue with Stack Exchange code markup that I'm not seeing here.

– Daniel Andersson – 2014-03-31T06:37:34.110

Oh yes, it works) Possibly there were some troubles with bash markup or non-printing characters' issues. I need to improve my skills in script-writing:) – Suncatcher – 2014-03-31T19:36:44.013