VideoHelp Forum
+ Reply to Thread
Results 1 to 18 of 18
Thread
  1. Member
    Join Date
    Nov 2013
    Location
    United States
    Search PM


    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.

    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
    Thank You!
    Quote Quote  
  2. 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/
    To locate your MKVs try
    Code:
    find /videos/movies/testa/ -type f -name '*.mkv'
    The name search is case sensitive so if you have some *.MKV files you will have to do another search. Do a test with a couple files in a test folder first to avoid nasty surprises.

    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
    OR if you don't want to mess too much with the command line you can install WinFF from the repository and select all add files for each directory.

    To remove the remaining files you can try
    Code:
    find /videos/movies/testa/ -type f -name "*.mkv" -exec rm -f  {} +
    Test everything first
    Quote Quote  
  3. Member
    Join Date
    Nov 2013
    Location
    United States
    Search PM
    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
    Quote Quote  
  4. 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
    Sounds like a plex database/library problem, post your solution when you find one. Check in vlc or mediafinfo that the old name is gone.
    Quote Quote  
  5. Member
    Join Date
    Nov 2013
    Location
    United States
    Search PM
    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,
    Quote Quote  
  6. Member
    Join Date
    Nov 2013
    Location
    United States
    Search PM
    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,
    Quote Quote  
  7. 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
    edit : this might come in handy, as usual test before applying any command.
    https://techblog.jeppson.org/2016/01/use-ffmpeg-to-batch-convert-video-files/
    Last edited by ackboo; 7th Feb 2016 at 22:09.
    Quote Quote  
  8. Member
    Join Date
    Nov 2013
    Location
    United States
    Search PM
    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".
    Quote Quote  
  9. 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.
    Quote Quote  
  10. Member
    Join Date
    Nov 2013
    Location
    United States
    Search PM
    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
    When run it finds the first .mkv and an existing .mp4 ..."already exists. Exiting.", then spawns another process and moves onto the next movie, which again finds the .mkv and an existing .mp4 "already exists. Exiting." So this is as intended. It continues until and ends within a few seconds where it reaches that last .mp4 file found, exit's and that's it, it does not spawn another process. It is as though it is looking only for .mkv's and .mp4's together so that it can see them, acknowledge them and exit. As no other folders have the two together, (.mkv+.mp4) in the same folder, it has nothing to do.....(just a thought).

    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.
    Quote Quote  
  11. 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.
    Quote Quote  
  12. Member
    Join Date
    Nov 2013
    Location
    United States
    Search PM
    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...
    Quote Quote  
  13. 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?
    Quote Quote  
  14. Member
    Join Date
    Nov 2013
    Location
    United States
    Search PM
    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....
    Quote Quote  
  15. Try to run your avconv command manually on one the unprocessed mkv, the one just after the last mkv + mp4 pair
    Code:
    avconv -i inputfile.mkv -format mp4 -vcodec copy -acodec ac3 -strict -2 -sn -movflags +faststart$  output.mp4
    Are there any special characters in the filename?
    Quote Quote  
  16. Member
    Join Date
    Nov 2013
    Location
    United States
    Search PM
    unfortunately, there is no information as to what movie avconv was looking to process next, as it is processing them in no specific order
    Quote Quote  
  17. 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 {} +
    if there are too many items you can redirect the output to a text file
    Code:
    cd /videos/movies/kids
    Code:
    find /videos/movies/kids -type f -name "*.mkv" -exec ls -lah {} + > aalist.txt
    Quote Quote  
  18. Member
    Join Date
    Nov 2013
    Location
    United States
    Search PM
    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 now i'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.
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!