Hello Morgan,
Thank you very much for taking the time to look into this.
I wish I saw your script before writing mine.
The script works just fine but what I what I found out is that actually some metadata gets lost/changed during the ffmpeg conversion even though the -map_metadata 0 option is activated.
Bento4 SDK Suit can copy the atoms containing the metadata from the original file to the compressed one.
So I am trying to add the following actions to the script:
First of all, download bento4 SDK and add the arguments to the script: (I guess you can skip this step if you compiled it from sources.) mp4extract="/path/to/static/build/mp4extract" mp4edit="/path/to/static/build/mp4edit"
Then # Do the conversion. count="1" for f in "$@"; do
Step 1 - Extraction of the udta atom from each video file (filename.mp4) to a corresponding txt file (filename.txt): mp4extract moov/udta "${f}" ${f%.*}.txt
Step 2 - Compression of each original video file (filename.mp4) to filename_suffix.mp4 using ffmpeg: ffmpeg -i "${f}" -y -f mp4 -c:a "${ca}" -b:a "${ba}" -c:v "${cv}" -crf "${crf}" -preset "${preset}" -map_metadata 0 "${f%.*}_${cv}_${crf}_${preset}.mp4" || exit 1
Step 3 - Import the metadata from the txt files (filename.txt) to the compressed mp4 videos (filename_suffix.mp4).
Here is the code I wrote for this: for FILE in *.txt; do BASE=${FILE%.txt} if [ -e "$BASE.txt" ]; then echo -e "\033[1;34m Importing metadata from "$BASE.txt" to "$BASE.mp4" / "$BASE_gps.mp4"\033[0m" mp4edit --insert moov:"$BASE.txt" "$BASE.mp4" "$BASE_gps.mp4" fi done
but I don’t know if the code actually works. I simply don’t get to the step 3 because the step 1 does not work.
The command works just fine when I run it in the terminal and drag a file there, e.g. /path/to/Bento4-SDK-1-5-0-614.x86_64-unknown-linux/bin/mp4extract moov/udta '/path/to/filename.mp4' '/path/to/filename.txt'
but if mp4extract being called via a script I get this error: Open: open(moov/udta) failed (src/mp4file.cpp,398)
I am wondering why there is a difference between running mp4extract in the terminal and calling it via a bash script. Any ideas? https://www.bento4.com/
@Morgan_Hardwood I just finished working on version 1.0 of the script and wondering what is the right way to publish it on GitHub. I created a repository but not sure how I can upload a file there. Also I can create a gist but I don’t know what gist is. Gists are a pain in the neck to find from mobile device as well. Can you advise ?
@Andrius maybe its better if you first experiment with your own repo for some weeks before pushing to pixls to avoid breakage. There are plenty of resources - especially the Git Handbook and youtube. Happy to help if you run into trouble. Creating a git repo is easy.
I can pull changes from the web via git pull origin master but when I am trying to push them back from my PC to the server git push origin master I get a message Everything up-to-date but the changes are not being pushed back to git.
Does it matter how big the changes are? I just changed license from GPL to GPLv3 to check the functionality
That’s the one I made on the webpage. I was able to pull it from my laptop. But I can’t push anything back to GitHub though… it says everything is up to date even though I know it is not
Hello, I figured out how to use GitHub and released v1.0-bash of my script called compress2mp4.
The main purpose of the script is compressing video files created by cameras and mobile phones to free up space on HDD.
Compression is lossy; output format is h.264; container is mp4.
Compression ratio really depends on the input file and is usually higher for MOV files created by digital cameras and Apple iPhones and lower for MP4 files created by Android phones (probably because Android already applied some sort of compression to them).
Script consists of few modules:
Basic module - compress using ffmpeg. Constant Rate Factor set to 23 by default (the value recommended by FFmpeg team).
Check compression ratio. If it is less than 1.5 the file will be re-compressed with CRF 26 (lower quality, lower output file size)
Import [udta] atom from the original to the output using Bento4 SDK. This step required if you want to be able to see your compressed videos on the map from your Android phone. Apparently some information gets lost during the compression even though “-map_metadata 0” option is activated. For videos created by iPhones you will have to copy and paste the [meta] atom (moov/meta) instead.
Verify GPS information of the output files using MediaInfo. Apparently ffmpeg is not able to catch the error.
Write FileModifyDate from Quicktime Create Date. This step required if you want to see compressed videos in correct chronological order in digiKam. Apparently exiv2 which digiKam relies on is capable of reading metadata from some certain video formats only (none of the video files I had were detected correct).
Delete temporary files and move originals to specified folder (I will be deleting originals from that folder after a year or so).
One can delete unwanted modules.
Future plans:
Rewrite the script in python. Just because I want to learn python for my second hobby - automation where python being used extensively.
Add an ability to write GPS information into sidecar xmp files that will be recognized by digiKam.
Special thanks to @Morgan_Hardwood who helped me to get started.
Any comments and critics are welcome.
Stay tuned for future releases!
@Morgan_Hardwood while using your script with 422 material ffmpeg issued the advice
No pixel format specified, yuv422p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.
Not saying that’s something wrong with the script , but as I wanted a 420 x264, added the before mentioned -pix_fmt yuv420p. Not problem with 10bit streams ( AFAIK x264 also supports 10 bit) which gets the 8bit frog-mask
On a side note, while the extra info _libx264_23_slow might be useful say if one encodes with different parameters and wants to remember or compare different versions, on a practical scenario becomes “noise”; again this is just my personal view, but if I’m delivering a light copy to someone the extra long tail is confusing. I guess that changing ${cv}_${crf}_${preset} to just _copy or _x264 would do the trick. Nevertheless I felt like giving user feedback and thank you for sharing it =)
ffmpeg version 3.3
OSX 10.11.6
PS
Also had an error with libfdk_aac, but that was a missing library from ffmpeg’s compilation. brew install ffmpeg --with-fdk-aac solved it