Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: using bash to make a list of movie files with mediainfo

  1. #1
    Join Date
    Jan 2014
    Beans
    12

    using bash to make a list of movie files with mediainfo

    I have a directory of moviesI (avi).

    I would like to make a list of the movies using media info.

    Each line on the list should contain one movie on a line with the "Complete name" and 'Mastered date"

    Ultimately, I would like the list to be sorted by the "Mastered date"

    I have tried to write a bash script but have run into some issues.

    First, the output looks like below. It provides the absolute file name and I was want "20080706MVI_0011.AVI".
    Second, it provides the date from the lines between Complete name and Mastered date and I want it to ignore that information.
    My bash script follows below.


    Code:
    item: /media/MY_BOOK/avi/20080706MVI_0011.AVI: /media/MY_BOOK/avi/20080706MVI_0011.AVI: AVI
    Audio Video Interleave: 2.25 MiB
    19s 133ms: 988 Kbps
    SUN JUL 06 08:59:34 2008
    item: /media/MY_BOOK/avi/20080706MVI_0012.AVI: /media/MY_BOOK/avi/20080706MVI_0012.AVI: AVI
    Audio Video Interleave: 3.67 MiB
    32s 0ms: 962 Kbps
    SUN JUL 06 09:00:10 2008
    item: /media/MY_BOOK/avi/20080706MVI_0013.AVI: /media/MY_BOOK/avi/20080706MVI_00]
    Code:
    #!/bin/sh
    
    #
    #Just a script to print a list of movied by Complete name and Mastered date
    #
    
    for i in /media/MY_BOOK/avi/* ; do
        printf 'item: %s: ' "$i"
        mediainfo "$i" | sed -e '/Complete name/,/Mastered date/{s/.*: //p;};d' | sed -e '$!N;s/\n/: /'
    done

  2. #2
    Join Date
    Mar 2010
    Location
    India
    Beans
    7,351
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: using bash to make a list of movie files with mediainfo

    Quote Originally Posted by rioguia2 View Post
    First, the output looks like below. It provides the absolute file name and I was want "20080706MVI_0011.AVI".
    How about showing us the example of How you'd like it to be? In the meanwhile, how about this -
    Code:
    #!/bin/bash
    find /media/MY_BOOK/avi/ -type f -iname "*.avi" -printf 'Item : %-20.20f : %-80.80p\n' -exec mediainfo -f "{}" \; -printf '\n' | egrep 'Item|Mastered date' | sed '/$/ {N; s/\n.*: /: /}'
    I'm sure there should be a much easier and faster code for doing this, above one just works for me.

    Also, you may replace %-20.20f and %-80.80p with just %f and %p. The -20.20f makes sure the field width is exactly 20 characters, any output string longer than this would be trimmed to fit in, thus maintaining the formatting. Same for -80.80p.
    Varun
    Help others by marking threads as [SOLVED], if they are. (See how)
    Wireless Script | Use Code Tags

  3. #3
    Join Date
    Jan 2014
    Beans
    12

    Re: using bash to make a list of movie files with mediainfo

    Thanks. When i ran this, i got the following output. I'm thinking that some basename or other bash trick is necessary.

    Code:
    Item : film.avi             : /home/dir/export/film.avi                                                       : /home/dir/export/MVI_3942.AVI]

    I've been reading about RSYNC (which is where i hope to put the generated list to work). During that time, I found an explanation of generating a list (How to get file only file name with linux `find`) such that the files are stripped of their directory information. A list formatted like this is necessary to pass a list to rsync (using " --files-from") to generate a comprehensive collection of movies scattered over my computer.

    Code:
    find . -type f -iname '*.avi' -printf "%f\n" > /home/dir/avi_list.txt
    The output looks like this:
    Code:
    DV00002.avi
    MVI_4028.AVI
    I'm still interested in using the metadata to do the search but i have obviusly got a lot to learn.
    Last edited by rioguia2; February 13th, 2014 at 03:35 AM. Reason: changed dir name

  4. #4
    Join Date
    Mar 2010
    Location
    India
    Beans
    7,351
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: using bash to make a list of movie files with mediainfo

    So did you get what you wanted?

    If not, please manually type a few example lines (or list) to show us what kind of result you want.

    I'm not a bash expert, but we have a few scripting magicians here who may be able to suggest fast and simple solutions.
    Varun
    Help others by marking threads as [SOLVED], if they are. (See how)
    Wireless Script | Use Code Tags

  5. #5
    Join Date
    Mar 2006
    Location
    Williams Lake
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: using bash to make a list of movie files with mediainfo

    Moved to Programming Talk

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,247
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: using bash to make a list of movie files with mediainfo

    in what way you want to use the metadata to search?
    also example of mediainfo output for your .avi files would be nice, i tried running it on few files and they lacked the 'mastered date' field.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  7. #7
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: using bash to make a list of movie files with mediainfo

    This is not in bash, but since we're having a snow day today and I'm a bit bored - plus this seemed like a fun excersise - I wrote a perl script to do what you want. The script will read in a list of avi movies and use the 'mediainfo' program to read the movie metadata into a hash. Then the metadata is printed out in a tab delimited table containing a sorted list of movie titles and 'Mastered date' fields. You can go to a directory containing your movies and run the script like so:

    Code:
    ./media_catalog.pl *.avi #assuming you want to process all avi's in the current directory
    For the few I had lying around, I get this:

    Code:
    $ ./media_catalog.pl *.avi
    Movie   Mastered Date
    MVI_0181        Sun Apr 17 01:25:06 2005
    MVI_0183        Sun Apr 17 01:29:20 2005
    MVI_0184        Sun Apr 17 01:29:58 2005
    MVI_0185        Sun Apr 17 01:30:32 2005
    Packing Day 001 Sun Apr 17 01:30:32 2005
    I thought it might be handy to do this in perl as you'll now have a hashed data structure that you can use to print out other fields besides 'Mastered date' if you choose. Line 92 of the program specifically creates the new hash with the movie title and Mastered date info. We can expand that hash to contain more information if you want, and then just change how it's printed in the next code block.

    Hope you find this somewhat helpful.

    Code:
    #!/usr/bin/perl
    # media_catalog.pl
    # Using the mediainfo program, read in a list of movies and generate an output table of
    # sorted movie titles and mastered dates.
    #
    # For now just print out the Mastered Date field.  Can add command line options to add 
    # additional metadata as needed.
    #
    # 2/13/2014
    ###########################################################################################
    
    
    use warnings;
    use strict;
    use Getopt::Long;
    use File::Basename;
    use Data::Dumper;
    
    
    ( my $scriptname = $0 ) =~ s/^(.*\/)+//;
    my $version = "v1.0.0";
    my $description = <<"EOT";
    Using the mediainfo program, read in a list of movies and generate an output table of sorted
    movie titles and matered dates.  The mediainfo metadata is stored in a hash that can be used
    to print out other movie inforamtion depending on what is required.
    EOT
    
    
    my $usage = <<"EOT";
    USAGE: $scriptname [options] <input_file>
        -o, --output    Send output to custom file.  Default is STDOUT.
        -v, --version   Version information
        -h, --help      Print this help information
    EOT
    
    
    my $help;
    my $ver_info;
    my $outfile;
    
    
    GetOptions( "output=s"    => \$outfile,
                "version"     => \$ver_info,
                "help"        => \$help )
            or print $usage;
    
    
    sub help {
            printf "%s - %s\n\n%s\n\n%s\n", $scriptname, $version, $description, $usage;
            exit;
    }
    
    
    sub version {
            printf "%s - %s\n", $scriptname, $version;
            exit;
    }
    
    
    help if $help;
    version if $ver_info;
    
    
    # Make sure enough args passed to script
    if ( scalar( @ARGV ) < 1 ) {
        print "ERROR: Not enough arguments passed to script!\n\n";
        print "$usage\n";
        exit 1;
    }
    
    
    # Write output to either indicated file or STDOUT
    my $out_fh;
    if ( $outfile ) {
            open( $out_fh, ">", $outfile ) || die "Can't open the output file '$outfile' for writing: $!";
    } else {
            $out_fh = \*STDOUT;
    }
    
    
    #########------------------------------ END ARG Parsing ---------------------------------#########
    
    
    my @files = @ARGV;
    my (%metadata, %results);
    my @sections;
    my @headers = qw{ General Video Audio };
    
    
    for my $file ( @files ) {
        (my $movie_title = basename($file) ) =~ s/\.avi//; 
    
    
        # Read in the metadata from the 'mediainfo' program
        open( my $mi_data, "mediainfo $file |" ) || die "Can't open the stream: $!";
    
    
        {
            local $/ = '';
            @sections = map { s/(General|Video|Audio)\n//; $_ } <$mi_data>;
        }
    
    
        # Store all of the metadata collected into a hash
        for my $i ( 0..$#sections ) {
            my @data = split( /\n/, $sections[$i] );
            $metadata{$headers[$i]} = {map { split( /\s+: /, $_ ) } @data}; 
        }
        $results{$movie_title} = $metadata{'General'}->{'Mastered date'};
    }
    
    
    # Print out a nice display of the data
    print $out_fh "Movie\tMastered Date\n";
    for my $title ( sort keys %results ) {
        print $out_fh "$title\t$results{$title}\n";
    }

  8. #8
    Join Date
    Jan 2014
    Beans
    12

    Re: using bash to make a list of movie files with mediainfo

    Wow! Thanks for all the responses.

    Regarding the perl script, it worked great.
    I copied the script into folder containing .AVI's. and made it executable (chmod +).

    I called the script like this:
    Code:
    perl media_catalog.pl -o media_catalog_output.txt *.AVI_
    I got the following:

    Code:
    MVI_0003.AVI    TUE JUL 12 18:52:44 2011
    MVI_0024.AVI    THU NOV 11 12:32:40 2010
    MVI_0025.AVI    THU NOV 11 12:33:35 2010
    MVI_0048.AVI    THU NOV 11 13:09:46 2010
    MVI_0049.AVI    THU NOV 11 13:10:32 2010
    MVI_0088.AVI    SAT NOV 20 12:07:46 2010
    MVI_0090.AVI    THU AUG 25 19:44:23 2011
    MVI_0091.AVI    FRI AUG 26 07:26:22 2011
    I really appreciate you sharing your expertise. I thought it would be great to be sorted by date so I piped it through sort based on a suggestion like this:
    Code:
    perl media_catalog.pl *.AVI | sort -nk6 > media_catalog_output.txt
    and got it to look like this:

    Code:
    DSCN0285.AVI
    Movie   Mastered Date
    MVI_0467.AVI    Fri Sep 12 07:01:45 2003
    MVI_1876.AVI    Tue Apr 20 20:44:59 2004
    MVI_1877.AVI    Tue Apr 20 20:48:10 2004
    MVI_1881.AVI    Wed Apr 21 19:20:09 2004
    MVI_1902.AVI    Sat Apr 24 13:09:54 2004
    MVI_3416.AVI    Sat Feb 05 09:51:27 2005
    Note: that your perl script helpfully recorded AVI files that didn't have the metadata (at the top of this list) so that i can just add that later.

    I really appreciate your work and having seen what this can do, I wonder if it would be difficult to rename the files in YYYY_MM_DD_hh_mm_ss or to allow it to recurse through a directory tree?

    I also like the way it was easy to take the output and pipe it. Maybe leaving the perl script as it is (simple and flexible) and then just do the renaming through bash.
    Last edited by rioguia2; February 16th, 2014 at 10:36 PM. Reason: format, typo

  9. #9
    Join Date
    Mar 2010
    Location
    India
    Beans
    7,351
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: using bash to make a list of movie files with mediainfo

    If you are happy with the answers and have no more questions, please consider marking the thread as [SOLVED] using the "Thread Tools" link above the top post. It helps others as well by indicating that the thread contains something that works for the topic. Thanks !
    Varun
    Help others by marking threads as [SOLVED], if they are. (See how)
    Wireless Script | Use Code Tags

  10. #10
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,247
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: using bash to make a list of movie files with mediainfo

    I wonder if it would be difficult to rename the files in YYYY_MM_DD_hh_mm_ss or to allow it to recurse through a directory tree?
    what do you mean by that because that sentence is not exactly clear?
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •