LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-16-2018, 02:58 PM   #1
daweefolk
Member
 
Registered: Sep 2009
Distribution: raspbian
Posts: 94

Rep: Reputation: 15
How to find mkv files; convert to mp4 with ffmpeg


I'm not a complete newbie to linux, but here's my problem:
I'm trying to write a script that would use ffmpeg to convert mkv or avi files in a specific directory (and subdirectories) to .mp4.

I know I'll need the ffmpeg line
Code:
(ffmpeg -i filename.mkv -c:v libx264 -maxrate 8M -bufsize 1M filename.mp4)
and likely a 'find' command
Code:
(find /home/daweefolk/Movies -name "*.mkv"?)
but don't know how to hook them together, or if they're even right.

I just want the file converted, with the extension changed but basename the same, in the same directory.

Can I get some help with putting the script together? I plan to set it up in cron to run automatically

Last edited by daweefolk; 04-16-2018 at 02:59 PM.
 
Old 04-16-2018, 03:10 PM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
These files are in the same directory?

Examples:
Code:
for i in *; do
    ffmpeg -i "$i" <options here> ${i%.*}.mp4
done
If they use codecs that can be put into a .mp4 container, and you don't want to reencode
Code:
ffmpeg -i file.mkv -c:a copy -c:v copy ouput.mp4
Quote:
I just want the file converted, with the extension changed but basename the same, in the same directory.
Get the basename for files in dir
Code:
for i in *; do echo ${i%.*}; done
 
Old 04-16-2018, 03:41 PM   #3
daweefolk
Member
 
Registered: Sep 2009
Distribution: raspbian
Posts: 94

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by teckk View Post
These files are in the same directory?

Examples:
Code:
for i in *; do
    ffmpeg -i "$i" <options here> ${i%.*}.mp4
done
If they use codecs that can be put into a .mp4 container, and you don't want to reencode
Code:
ffmpeg -i file.mkv -c:a copy -c:v copy ouput.mp4
Get the basename for files in dir
Code:
for i in *; do echo ${i%.*}; done
As I plan to run this from cron, I was hoping to get a script that would start in my ~/Movies directory and recurse
 
Old 04-17-2018, 01:19 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
well be aware that you should run your script as a normal user, not root!

anyhow, a quick change to the previous:

Code:
find ~/Movies -iname '*.mkv' | while read line
do
    ffmpeg -i "$line" ...
    etc....
done
 
Old 04-17-2018, 01:20 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
well be aware that you should run your script as a normal user, not root!

anyhow, a quick change to the previous:

Code:
find ~/Movies -iname '*.mkv' | while read line
do
    ffmpeg -i "$line" ...
    etc....
done
 
Old 04-19-2018, 04:22 PM   #6
Prehistorik
Member
 
Registered: Oct 2006
Location: St. Petersburg, Russia
Distribution: Ubuntu
Posts: 31

Rep: Reputation: 1
If you're going to use cron, then there are some other things to consider:

1. Cron limits $PATH to /(s)bin and /usr/(s)bin, so you'll have to use full path to ffmpeg if you have compiled it from source

2. The script must check if the output file already exists and skip the input if it does. Otherwise all files will be encoded over and over again on every run.

You can use a simple file test:

Code:
if [ ! -e "$dest_file" ]; then
    # ... process the file
fi
Or, if source files can change and destinations must be updated:

Code:
if [ ! -e "$dest_file" -o "$src_file" -nt "$dest_file" ]; then
    # ... process the file
fi
3. Encoding is a time-consuming process so if there are many files to encode the previous batch might not be complete yet when the next one starts. I would suggest using some queueing mechanism like Task Spooler (ts):

http://vicerveza.homeunix.net/~viric/soft/ts/

This utility should be in most major distributions. In some cases it can be called a different name like "tsp".

It can also run multiple tasks in parallel, you can set maximum number of simultaneous processes with a command line option.
 
  


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] ffmpeg convert MKV to MP4 with silent audio track mackowiakp Linux - Newbie 2 05-13-2017 04:28 PM
Convert .mkv or .mp4 to .VOB and write via K3b. hack3rcon Linux - Software 11 02-07-2016 10:53 PM
[SOLVED] Convert mkv to mp4 in terminal - how? gael33 Linux - Newbie 13 11-30-2015 03:42 PM
Convert mkv to mp4 to play on PS3 ? robertpolson Linux - General 1 09-11-2007 03:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:06 PM.

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