FedoraForum.org - Fedora Support Forums and Community
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2008
    Location
    Witham, Essex, UK
    Age
    30
    Posts
    341

    Bash Script Needed!

    Hi Guys,
    I'm looking for someone that can cobble together a Bash script to automate extracting and splicing some audio files for me. I have a folder full of multi-part RAR'd video as follows:

    Video1 --> CD1 --> *.rar
    Video1 --> CD2 --> *.rar

    Inside the RAR is an AVI file, and I need to splice the two files together to create a full-length video.

    Rough algorithm is as follows:
    1) Extract CD1
    2) Extract CD2
    3) Splice
    4) Move spliced video to new folder

    There's 23 video folders, so I'd need a script to loop through each of these folders and do the above in each. I have ffmpeg and mencoder installed.

    I'm fairly good with scripting, but I'm not too keen on ruining about 35GB of video!

    Cheers,
    Craig
    Personal Website | Windows to Linux - Tips from Experience

    Desktop - Galileo
    Dual-Boot: Fedora 13 x64, Windows 7 x64
    Intel Core2Quad Q6600 @3.6GHz, 8GB PC2-8500 DDR2, ATI HD4870, 23" @ 1920x1080 + 20" @ 1600x900 (both DVI-D), 2TB ICH10R RAID0 array, Custom Watercooling

    Laptop - MacBook 5.1 ('08 Aluminium)
    Dual-Boot: Fedora 13 x64, Apple OS X
    Intel Core2Duo, 2GB 1066MHz DDR3, nVidia 9400M, 13" LCD @ 1280x800, 160GB SATA

  2. #2
    Join Date
    Jul 2007
    Posts
    371

    Re: Bash Script Needed!

    One problem with joining two videos together is that after the point where it's joined video and audio may go out of sync - this is due to the fact that sometimes the audio track is shorter than the video track. The solution is to delay the audio track of the second file by the amount that the audio and video track differ in the first file. I remember having done this in Virtualdub a long time ago. You'd have to join the video and audio track separately, then mux them together. Anyway good luck if you manage to make all this work within a single script.
    these command lines are like casino slot machines, every time I input commands NOTHING HAPPENS

  3. #3
    Join Date
    Aug 2009
    Location
    Waldorf, Maryland
    Posts
    7,346

    Re: Bash Script Needed!

    My suggestion, (I'm not familar with handling video data this way)
    If you want to test what you are doing, make a copy of one of your directories
    to test in. Then use the "script" utility to record what you are doing.

    Perform the steps you want - and let the script utility log file record what
    you do. Then you can use the log file to extract only the commands that
    do the work you want (removing the test verifications...)

    The end result is a script that works with only one directory.

    You can now use the script 35 times... or create another script that
    will set the working directory to each of the real directories and then
    invoke the other script.

  4. #4
    Join Date
    Jan 2008
    Location
    Witham, Essex, UK
    Age
    30
    Posts
    341

    Re: Bash Script Needed!

    Just to update people, I managed to write a script to do what I wanted, but in PHP rather than Bash (string manipulation in Bash isn't my strong point!):

    PHP Code:
    <?php

    // Path to files
    $path "/tmp/Video";

    // Open the directory
    $dir opendir($path);

    $mtime microtime();
    $mtime explode(" ",$mtime);
    $mtime $mtime[1] + $mtime[0];
    $starttime $mtime

        
    // Loop through directory
        
    while (false !== ($file readdir($dir))) {
        
            
    // Ignore dot-files
            
    if (($file == ".") || ($file == "..") || ($file == "Final")) { continue;}
        
            
    // Extract name and year
            
    preg_match("/007(.*)\.(\d\d\d\d)/"$file$matches);
            
    $name trim(str_replace("."," ",$matches[1]));
            
    $year $matches[2];

            
    // Unrar CD1
            
    echo "Extracting $name CD 1\n";
            
    exec("unrar e -inul -y \"$path/$file/CD1/*.rar\" \"$path/$file/\"");
            
            
    // Unrar CD2
            
    echo "Extracting $name CD 2\n";
            
    exec("unrar e -inul -y \"$path/$file/CD2/*.rar\" \"$path/$file/\"");
            
            
    // Create Final Dir
            
    echo "Creating \"$year - $name\" Directory\n";
            
    mkdir("$path/Final/$year - $name");
            
            
    // Merge
            
    $merge_path "$path/$file/";
            echo 
    "Merging $name ...\n";
            echo 
    "\tUsing cat\n";
            
    exec("cd \"$merge_path\" && cat *.avi > \"$path/Final/$year - $name/$name-tmp.avi\"");
            echo 
    "\tUsing mencoder\n";
            
    exec("mencoder -forceidx -oac copy -ovc copy \"$path/Final/$year - $name/$name-tmp.avi\" -o \"$path/Final/$year - $name/$name.avi\"");
            
            
    // Cleanup
            
    echo "Cleaning up $name\n";
            
    exec("rm -Rf \"$path/$file\"");
            
    exec("rm -f \"$path/Final/$year - $name/$name-tmp.avi\"");
            echo 
    "---------------------------\n";
        }
        
       
    $mtime microtime();
       
    $mtime explode(" ",$mtime);
       
    $mtime $mtime[1] + $mtime[0];
       
    $endtime $mtime;
       
    $totaltime = ($endtime $starttime);
       
    $totaltime round($totaltime,1);
       echo 
    "Execution time: ".$totaltime." seconds\n";
       echo 
    "---------------------------\n";
        
    echo 
    "\n";

    ?>
    The basic algorithm is:

    1) Do string manipulation to get nice clean data (mine was formatted with dots instead of spaces and with the year of the film hidden within the folder name)
    2) Unrar each CD using unrar
    3) Merge the CDs using cat
    4) Fix the AVI headers and sync audio by using mencoder with the -forceidx flag

    The manipulation in step 1 is optional, but it allows me to save the files in a nice, neat format (I'm pedantic when it comes to my movie collection!).

    Any questions, by all means ask
    Personal Website | Windows to Linux - Tips from Experience

    Desktop - Galileo
    Dual-Boot: Fedora 13 x64, Windows 7 x64
    Intel Core2Quad Q6600 @3.6GHz, 8GB PC2-8500 DDR2, ATI HD4870, 23" @ 1920x1080 + 20" @ 1600x900 (both DVI-D), 2TB ICH10R RAID0 array, Custom Watercooling

    Laptop - MacBook 5.1 ('08 Aluminium)
    Dual-Boot: Fedora 13 x64, Apple OS X
    Intel Core2Duo, 2GB 1066MHz DDR3, nVidia 9400M, 13" LCD @ 1280x800, 160GB SATA

Similar Threads

  1. [SOLVED] bash script help needed please
    By snoopo71 in forum Using Fedora
    Replies: 4
    Last Post: 27th June 2010, 01:10 PM
  2. Bash Script Help
    By Jamwa in forum Using Fedora
    Replies: 5
    Last Post: 17th June 2008, 05:26 PM
  3. Convert bash script to perl script
    By homey in forum Programming & Packaging
    Replies: 1
    Last Post: 2nd September 2006, 04:24 AM
  4. [BASH] i need script
    By coxis in forum Programming & Packaging
    Replies: 2
    Last Post: 11th June 2005, 05:07 PM
  5. Bash Script on FC3
    By Clunixchit in forum Programming & Packaging
    Replies: 7
    Last Post: 8th April 2005, 09:29 PM

Tags for this Thread

Posting Permissions

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