I am looking to recursively convert my .MKV's to .MP4's with AC3 audio. The code below works, however, it only works for files located in one single directory..."testa". How can I have this convert .MKV files recursively, leaving them in the same folder? I can come back and manually remove the .MKV's after as I have knowledge of Linux to be dangerous.
Thank You!Code:#!/bin/sh #Change .MKV container to .MP4 and convert audio to AC3 for f in /videos/movies/testa/*.mkv; do avconv -i "$f" -format mp4 -vcodec copy -acodec ac3 -strict -2 -sn -threads 3 -movflags +faststart "${f%.*}.mp4"; done exit
+ Reply to Thread
Results 1 to 18 of 18
-
-
Inspired by
http://stackoverflow.com/questions/22410232/find-in-bash-to-convert-files-with-ffmpeg-...-being-changed
For more safety go to the directory in the terminal,
Code:cd /videos/movies/testa/
Code:find /videos/movies/testa/ -type f -name '*.mkv'
To execute a program on the found files do
Code:find /videos/movies/testa/ -type f -name "*.mkv" | while read f; do avconv -i "$f" -format mp4 -vcodec copy -acodec ac3 -strict -2 -sn -threads 3 -movflags +faststart "${f%.*}.mp4" < /dev/null; done
To remove the remaining files you can try
Code:find /videos/movies/testa/ -type f -name "*.mkv" -exec rm -f {} +
-
Cheers, the code works great. It takes the first audio track, which in most cases is English. For those that are not, I will have to identify those ahead of time and run a separate script and use the "map" command to select the audio track that is English. Not sure how to resolve the next item though, when the .mp4 is generated and Plex reads the file, it is pulling the name of the file from the Metadata and not from the title...have to go research on that...
thanks again -
Not sure how to resolve the next item though, when the .mp4 is generated and Plex reads the file, it is pulling the name of the file from the Metadata and not from the title
-
Resolved the issue in Plex:
Within Plex under Server>Settings>Agents>Movies> Freebase and TheMovie Database... drag 'Local Media Assets' below both Freebase and TheMovieDatabase. This will ensure it uses information from these rather from the local media itself.
thanks, -
Update:...the code provided worked great...however, avconv/ffmpeg encountered an error in one .mkv file and err'd out. Upon re-running the command, it will exit showing that the .mp4 file already exists. Within avonv/ffmpeg you can use either the -y switch to automatically overwrite, or the -n to not overwrite. The problem is that when you use the -n switch, the application aborts and exits....i.e., there is no 'Skip' option.
Looking for help in skipping existing .mp4 files.
Background: Why am I converting...I have a Plex server that several family members connect to. As they have various devices, Roku, X-Box, Fire, Chromecast, I need a format that is almost universal that can "Direct Play" rather than the server doing the work to convert and then stream....
thanks, -
Remove the problematic mkv file and the aborted mp4 and rerun the command with -n ? It should skip the existing mp4 files and resume the encoding of the not yet converted mkvs ? Or am I missing something ?
Code:avconv -i "$f" -n
https://techblog.jeppson.org/2016/01/use-ffmpeg-to-batch-convert-video-files/Last edited by ackboo; 7th Feb 2016 at 22:09.
-
The -n switch simply means...abort not skip. It will not overwrite the .mp4 file, but will not skip and keep going either, instead it will abort the rest of the job and quit....there is not switch for "Skip".
-
There is no skip flag in ffmpeg but you don't need it while running the code provided. It will launch a new ffmpeg for each file found by the find command. So if the mp4 file already exists ffmpeg will abort and the code provided will skip to the next mkv file. Make a test in another folder, put 2 mkv in it, run the code, add a new file, run the code with the -n flag. The code should skip the 2 already processed files and encode the third one.
-
This is what it looks like now it has the -i "$f" -n
find /videos/movies/kids -type f -name "*.mkv" | while read f; do avconv -i "$f" -n -format mp4 -vcodec copy -acodec ac3 -strict -2 -sn -movflags +faststart$ "${f%.*}.mp4" < /dev/null; done
File '/videos/movies/kids/Movie 9 (2012)/Movie 9 (2012).mp4' already exists. Exiting.
File '/videos/movies/kids/Movie 10 (2014)/Movie 10 (2014).mp4' already exists. Exiting. -
The program is working as intended, it starts from the beginning of the directory, looks for existing files and skips to the next. At some point it will reach an unprocessed mkv with no corresponding mp4 and encode it. Move the problematic mkv to another folder and let the program run. Depending on the number of files to examine it might take a while. Bash scripts are not fast.
It does continuously show these messages right?
edit : if it does not work maybe there is a problem with that last file, its name or content. Try to simply run avconv on it to see if there is an error message.Last edited by ackboo; 8th Feb 2016 at 12:01.
-
The program stops at the last .mkv file with an associated .mp4 file. moving that .mkv file to a different folder and restarting the program results in the program stopping at the same point except one movie ealier...
-
Is there a message before it exits?
What is the output of avconv -version (first line)?
Try to test the -n flag in a test folder with known good files, copy some already processed mkv, run the program, copy some more already processed mkv, run the program with the -n flag. What is the result? Are the mp4 created? Are the first files skipped? -
I created a directory "Failed" with sub directories 1,2,3,4 underneath... in directories 1 and 2 i put a .mkv and its .mp4 file. in 3 and 4 just the .mkv file. The script skips the first two directories, correctly identifying the .mp4 file. It then skips to directories 3 and 4 and proceeds to create the .mp4 files as scripted.
However, the same script with only the path modified quits after it finds the last .mkv file and .mp4 file pair.
There is no error, only *.mp4' already exists. Exiting.
makes no sense.... -
Try this, all the files and directories should have the same owner and the same permissions
Code:find /videos/movies/kids -type f -name "*.mkv" -exec ls -lah {} +
Code:cd /videos/movies/kids
Code:find /videos/movies/kids -type f -name "*.mkv" -exec ls -lah {} + > aalist.txt
-
ok...so anyone reading this thread in the future will say...."you fukin' moron"....doh!, so here's what i've done. The "Kids" movies only has about 12 or so .MKV's the rest are already .MP4's....wrong audio format...but still .MP4's, so the script exiting was doing exactly what it should have done as it was finished....i.e., i'm a moron...
thanks...
So nowi'll work on copying the existing .mp4's and converting the audio to AC3, then come back and remove the .mp4's.
sorry doesn't quite cover it....but, sorry.
Similar Threads
-
MkvТоMp4 v0.224 - rapid tool for repack Mkv to Mp4
By oreons in forum Video ConversionReplies: 808Last Post: 7th Mar 2022, 01:43 -
Mux Batch convert MKV to MP4 with subtitles in Linux
By coquex in forum Video ConversionReplies: 7Last Post: 17th Jun 2016, 17:47 -
Regarding Convert MKV or AVI to MP4 with Render in - Ubuntu Linux CLI
By baoky in forum Video ConversionReplies: 0Last Post: 17th Sep 2014, 06:59 -
Plain English Guide to MP4Box and Repacking MKV-MP4, MP4-MP4 (M4V).
By raregrit in forum Video ConversionReplies: 3Last Post: 8th Jul 2013, 12:52 -
MKV guide, Play MKV, MKV to AVI, MKV to DVD, MKV to MP4, MKV to Blu-ray
By Baldrick in forum Newbie / General discussionsReplies: 55Last Post: 29th Jun 2012, 11:19