Transcode JPG to lower resolution

Help
Petr Cerny
2011-10-08
2013-05-30
  • Petr Cerny
    Petr Cerny
    2011-10-08

    Hello!
    I wonder how I can transcode JPG files to lower resolution before they are sent to TV? My photos are around 9MB each and MediaTomb is connected to TV over 100MBit network so browsing my albums is painfully slow.
    TV is just fullHD (1920x1080px), so I would like to convert images from 4672×3104px to 1633x1080px which could result in about 15x smaller images.

    What I did so far:

    /etc/mediatomb/config.xml:

      <transcoding enabled="yes">
        <mimetype-profile-mappings>
          <transcode mimetype="image/small-jpeg" using="jpeg-small"/>
        </mimetype-profile-mappings>
        <profiles>
          <profile name="jpeg-small" enabled="yes" type="external">
            <mimetype>image/jpeg</mimetype>
            <agent command="/opt/jpeg-resize.sh" arguments="%in %out"/>
            <accept-url>no</accept-url>
            <first-resource>yes</first-resource>
            <hide-original-resource>no</hide-original-resource>
            <buffer size="4000000" chunk-size="20000" fill-size="20000"/>
            <resolution>1920x1080</resolution>
          </profile>
        </profiles>
      </transcoding>
    

    peta@gethexis ~ $ cat /opt/jpeg-resize.sh

    1
    2
    3
    4
    5
    #!/bin/bash
    echo "---------" >> jpeg-resize.log
    echo "Input  "$1 >> jpeg-resize.log
    echo "Output "$2 >> jpeg-resize.log
    cat "$1" | mogrify -resize 1920x1080 - > "$2"
    

    But it does not work. It seems the transcoding script does not get launched, because /opt/jpeg-resize.log gets never filled. On my TV, I always get the image in full resolution instead of smaller one.

    Have anyone tried anything similar? I am happy to provide any more info required.

    Thanks in advance!
    Peter

     
  • Petr Cerny
    Petr Cerny
    2011-10-08

    Correction: Script gets launched correctly and if I use it like that it works:
    #!/bin/bash
    echo "------" >> /opt/jpeg-resize.log
    echo "Input  "$1 >> /opt/jpeg-resize.log
    echo "Output "$2 >> /opt/jpeg-resize.log
    cat "$1" > "$2"

    So my question is, how can I use mogrify to get a file from stdin and output it to stdout? The problem is that common mode of operation is mogrify -resize 1920 file.jpg which results in file.jpg being overwritten…

     
  • Petr Cerny
    Petr Cerny
    2011-10-08

    Ok… Problem solved… Hopefully it can help someone…

    /opt/jpeg-resize.sh:

    1
    2
    3
    4
    5
    #!/bin/bash
    echo "---------" >> /opt/jpeg-resize.log
    echo "Input  "$1 >> /opt/jpeg-resize.log
    echo "Output "$2 >> /opt/jpeg-resize.log
    cat "$1" | convert - -quality "75" -resize "1920x1080" fd:1 > "$2"
    
     
  • Hi,

    thanks for the solution.

    I just wanted to add, that the pipe is unnecessary. The pattern
    cat SINGLE_FILE | COMMAND OPTIONS ARGUMENTS
    can always be replaced by
    COMMAND OPTIONS ARGUMENTS < SINGLE_FILE
    saving a bit of time and memory.

     

  • Anonymous
    2011-12-24

    Thanks for sharing, works for me.

     
  • joaquintopiso
    joaquintopiso
    2012-04-08

    Thanks for sharing. It worked for me in Ubuntu 11.10 Oneiric, although I had to make some changes.

    I've spent some time until I have realized  that:

    <transcode mimetype="image/small-jpeg" using="jpeg-small"/>
    

    should be:

    <transcode mimetype="image/jpeg" using="jpeg-small"/>
    

    I also found that the shown resolution on uPnP clients is the original one (not the low res one)  unless the <hide-original-resource> is set to yes.

    Invoking mediatomb in debugging mode from the terminal with
    sudo mediatomb -D
    and looking for "ProcessExecutor(): Launched process /opt/jpeg-resize.sh" helped to solve the problem.

    Just in case anyone is struggling with the same problem.