LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-20-2015, 03:12 PM   #1
S1NN3R
LQ Newbie
 
Registered: Oct 2009
Posts: 27

Rep: Reputation: 15
Need help with running a command string recursively


I'm still little new with bash commands and I've hit a brick wall. Recently I found you don't have to re-encode MKV's if you want to turn them into MP4's, you just simply change the container. I'm using the program FFMpeg to accomplish this. The below command looks in the current folder for any MKV and changes the container to MP4, it then goes through and renames the files.

Code:
for i in *mkv; do ffmpeg -i "$i" -codec copy "$i".mp4; rename 's/\.mkv\.mp4$/\.mp4/' *mkv.mp4;done
It works great, however what this means is I have to navigate into each TV show, then each season, and run the string. As it takes a little time to run it's becoming quite tedious to babysit the progress, then when it finishes change to the next folder, rinse and repeat. This is repetitive task I know that can be scripted to save time.

My file hierarchy looks like this:

Code:
-TV
 --Seinfeld
 --Star Trek TNG
    ---Season 01
    ---Season 02
            Episode 01
            Episode 02
            Episode 03
            ...
    ---Season 03
    ---Season 04
    ...
 --Star Wars Clone Wars
What I want is to run the command from the root TV folder and it recursively to go through each subfolder and run it's conversion operation. Whether there's some code I can add to the original string to do this, or if I have to write a little script file, whatever is more effective. I know this is probably a simple answer for someone with experience. Any help would be appreciated, thanks!
 
Old 08-20-2015, 03:19 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Code:
find . -type f -name "*.mkv" -exec ffmpeg -i '{}' -codec copy '{}'.mp4 \;
 
Old 08-20-2015, 03:32 PM   #3
dimonic
LQ Newbie
 
Registered: Aug 2013
Posts: 24

Rep: Reputation: Disabled
Recursive descent

So what you want to do could be done in a couple of ways (curse you Linux with your many ways to skin a cat).

One way is to use find

Code:
$ find . -name "*.mkv" -exec ffmpeg -i {} -codec copy `basename {} .mkv`.mp4 \;
Another would be to parse your command line for files and then test if directories and recurse using a shell function

Code:
#!/bin/sh

function parse_dir()
{
    for file in $*
    do
        if [ -d "$file" ]
        then
            cd "$file"
            parse_dir *
        else
          ffmpeg -i "$file" -codec copy `basename "$file" .mkv`.mp4
        fi
    done
}

parse_dir $*

Note that I am using basename to save the rename after each ffmpeg.

Last edited by dimonic; 08-20-2015 at 03:33 PM. Reason: adding note on basename
 
Old 08-21-2015, 07:16 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You could also augment your current script:
Code:
shopt -s globstar
shopt -s nullglob

for i in /TV/**/*.mkv; do
  ffmpeg -i "$i" -codec copy "$i".mp4

  rename 's/\.mkv\.mp4$/\.mp4/' *mkv.mp4
done
Untested but you should get the idea. It also assumes TV is at the root level, so obviously put in the appropriate path
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] appending number at the end of string, recursively udiubu Linux - Newbie 5 05-15-2012 01:22 PM
Bash script to remove string from all text files, recursively iacchi Programming 9 04-27-2012 05:22 AM
Change a string recursively in files located in folders and subfolders Drigo Linux - Newbie 2 07-22-2011 01:21 PM
Search a string recursively under one directory say_hi_ravi Programming 6 07-09-2008 04:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration