I want to convert any files in a large media collection that are not encoded with H264. In order to do that I need a tool to list all the files, I need to convert. There are a lot of files to go through. The search needs to be recursive as files are stored in the following format /media/tv shows/show name/season 01/filename.extension

  • What have you tried so far and what are you having issues specifically implementing? Do you only need to list or sort out non-H264 encoded media files? – kemotep Jan 12 at 21:55
  • @kemotep I have tried ffmpeg -i which tells me the video encoding but I'm not sure how to run that command on all files recursively but only return the file name if it's not encoded with h264. – cheesemarathon Jan 14 at 18:38
  • So if I understand you correctly, you need to search through a large directory structure of video files, figure out what encoding the video files used, and then list the output (most likely to a file) of every instance of a file that does not use H264 encoding? – kemotep Jan 14 at 19:19
  • Exactly @kemotep! – cheesemarathon Jan 16 at 9:34
  • I think I can have a script for you later today. – kemotep Jan 16 at 11:46

Here is what I have come up with so far. Note that this is a rough approximation of what you should do.

First, find all of your video files and output them to a file to be read, you can skip this part and just pipe the results to the next step.

find path/to/videos/directory -name \*.mp4 >> /tmp/videoList.txt

This finds all .mp4 files in all the subfolders of your media directory and puts them in a file.

Next, you can use a tool as shown in this example to probe what encoding each video file uses. In this case we are looking for h264.

while read p; do
mediainfo $p | grep -v h264 | grep .mp4 | \
  cut -f2 -d: >> /tmp/h264List.txt
done </tmp/videoList.txt

This example creates a file ,/tmp/h264List.txt, that has all the files that use h264 encoding. From here you can do a compare and find all of the non-h264 video files.

sort /tmp/videoFiles.txt /tmp/h264List.txt | uniq -u >> /tmp/nonh264List.txt

At this point you should have a file that has all of the non-h264 video files in the directories you searched in. All together it could look something like this:

Note that this is a rough example!


find /home/user/videos/tvshows -name \*.mp4 >> /tmp/videoList.txt

while read p; do
  mediainfo $p | grep -v h264 | grep .mp4 | \
  cut -f2 -d: >> /tmp/h264List.txt
done </tmp/videoList.txt

sort /tmp/videoList.txt /tmp/h264List.txt | uniq -u >> /tmp/nonh264List.txt

#Optional Cleanup, only if necessary
mv /tmp/nonh264List.txt /home/user/needToEncode.txt
rm /tmp/*.txt

Then from here you can do a similar loop through the needToEncode.txt file to run your desired ffmpeg encoding. You will need to change any paths to wherever your media is kept and the file extensions and encoding to whatever you are looking for.

I am going to include a link to the documentation for ffmpeg, as well as for mediainfo. You could also use the ffmpeg tool ffprobe as shown here to complete this task.

Please be sure to read through all links I have provided before attempting anything. This script may not be the most optimized or work exactly as you desire but I was able to use it to find all the video files, sort them, and save the output to a file.

  • This is fantastic thank you! I have it working mostly but sometimes the codec will be AVC. AVC = H264, so how would I change your code to find both h264 and AVC? – cheesemarathon Jan 18 at 11:01
  • @cheesemarathon you can change the mediainfo query to look for AVC so that part would become something like: mediainfo $p | grep -v AVC | grep .mp4 | cut -f2 -d: >> /tmp/h264List You can grep for any type of file, just remember to change all instances of that file format or extension in the script. If you have a lot of different file formats and codecs that you need to sort through please edit your post to include all of the different things that need to be sorted. – kemotep Jan 18 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.