# [Note] 20210723 Unknown MP4 MIME Type Source - ffmpeg course slide: http://slhck.info/ffmpeg-encoding-course - 一樣的 Bitrate,解析度高的畫質會比較好嗎? https://annkuoq.github.io/blog/2020-10-25-bitrate-vs-resolution/ >同 Bitrate,解析度越大 -> 畫質越差 同解析度,Bitrate 越大 -> 畫質越好 (但 Bitrate 大到一定的階段,畫質也不會一直無限好上去) 一樣的 Bitrate,不論解析度多大,檔案大小都一樣 - Manipulating audio channels https://trac.ffmpeg.org/wiki/AudioChannelManipulation ### Issue: The MIME type of the MP4 files uploaded from a customer is unusual files : https://drive.google.com/drive/folders/1GplZxQS6jGNLRv2tJLT_f5yGbwswJ7ut?usp=sharing - How to check the MMIE type? ```bash= $ file --mime-type <MP4_FILENAME> ``` ![](https://i.imgur.com/IiqGsWq.png) - The type is `application/octet-stream` According to [MDN_WEB_DOCS](https://developer.mozilla.org/zh-TW/docs/Web/HTTP/Basics_of_HTTP/MIME_types) >"Generic binary data (or binary data whose true type is unknown)" - How to solve this? - Related discussion: https://stackoverflow.com/questions/51059736/why-some-of-the-mp4-files-mime-type-are-application-octet-stream-instead-of-vid - Use ffmpeg to convert them ```bash= $ffmpeg -i C0081.MP4 -pix_fmt yuv420p -crf 18 N_C0081.MP4 ``` - After converting, detect the MIME type again ![](https://i.imgur.com/Pu060m7.png) --- ## Analysis the format change before & after >File: C0081.MP4 >File Size: 88.1MB ### Check the metadata of the C0081.MP4 ```bash= $ ffprobe -i C0081.MP4 ``` ```text Metadata: major_brand : XAVC minor_version : 16785407 compatible_brands: XAVCmp42iso2 creation_time : 2021-07-22T07:23:29.000000Z Duration: 00:00:13.02, start: 0.000000, bitrate: 54147 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709/bt709/iec61966-2-4), 1920x1080 [SAR 1:1 DAR 16:9], 51462 kb/s, 59.94 fps, 59.94 tbr, 60k tbn, 119.88 tbc (default) Metadata: creation_time : 2021-07-22T07:23:29.000000Z handler_name : Video Media Handler vendor_id : [0][0][0][0] encoder : AVC Coding Stream #0:1(und): Audio: pcm_s16be (twos / 0x736F7774), 48000 Hz, 2 channels, s16, 1536 kb/s (default) Metadata: creation_time : 2021-07-22T07:23:29.000000Z handler_name : Sound Media Handler vendor_id : [0][0][0][0] Stream #0:2(und): Data: none (rtmd / 0x646D7472), 491 kb/s (default) Metadata: creation_time : 2021-07-22T07:23:29.000000Z handler_name : Timed Metadata Media Handler timecode : 11:18:06:14 ``` ### Check the the metadata of the N_C0081.MP4 > N_C0081.MP4 is prodiced by this command > File Size: 16.4 MB > $ffmpeg -i C0081.MP4 -pix_fmt yuv420p -crf 18 N_C0081.MP4 ```bash= $ ffprobe -i N_C0081.MP4 ``` ```text Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf58.76.100 Duration: 00:00:13.02, start: 0.000000, bitrate: 10101 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709/bt709/iec61966-2-4), 1920x1080 [SAR 1:1 DAR 16:9], 9961 kb/s, 59.94 fps, 59.94 tbr, 60k tbn, 119.88 tbc (default) Metadata: handler_name : Video Media Handler vendor_id : [0][0][0][0] Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default) Metadata: handler_name : Sound Media Handler vendor_id : [0][0][0][0] ``` ### Copy the original video part & re-encode the audio part with other audio codec? Why should we do this? why shouldn't we copy it directly? We couldn't find a `pcm_s16be` with the video container. ```bash= ffmpeg -y -i C0081.MP4 -vcodec copy -acodec copy copy.mp4 ``` Result: ```text [mp4 @ 0x7f8e3e00d800] Could not find tag for codec pcm_s16be in stream #1, codec not currently supported in container Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument Error initializing output stream 0:1 -- Stream mapping: Stream #0:0 -> #0:0 (copy) Stream #0:1 -> #0:1 (copy) Last message repeated 1 times ``` Ref: - [FFMPEG Could not find tag for codec pcm_s16be with MP4](https://stackoverflow.com/questions/58286743/ffmpeg-could-not-find-tag-for-codec-pcm-s16be-with-mp4) >Apparently the MP4 norm does not include the pcm_s16be codec and hence ffmpeg refuses to create such a file. ### Method: Copy the video and re-encoded the audio part. This is s script to get the bit rate of the audio stream and set the output with the bit rate. ```bash= #!/usr/bin/env bash AUDIO_BITRATE=`ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 $1` echo $AUDIO_BITRATE ffmpeg -y -i $1 -acodec aac -b:a ${AUDIO_BITRATE}k -vcodec copy new-$1 ``` > -y : Overwrite output files without asking > -i : input file url > -acodec aac : set the audio codec with `aac` format > -b:a :set the audio bit rate of the output file > -vcodec copy : copy the video codec - Usage ```bash= $ chmod u+x script.sh $ ./script.sh C0081.MP4 ``` It would export to a file name `new-C0081.MP4` - Check the exported file Size: 83MB, Near to the original size file: https://drive.google.com/drive/folders/1GxiwamGg-acFHvTQujls5YbmIUBPfNJd?usp=sharing ```bash= $ ffprobe -i new-C0081.MP4 ``` - meta data ```text Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf58.76.100 Duration: 00:00:13.02, start: 0.000000, bitrate: 51832 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709/bt709/iec61966-2-4), 1920x1080 [SAR 1:1 DAR 16:9], 51462 kb/s, 59.94 fps, 59.94 tbr, 60k tbn, 119.88 tbc (default) Metadata: handler_name : Video Media Handler vendor_id : [0][0][0][0] Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 365 kb/s (default) Metadata: handler_name : Sound Media Handler vendor_id : [0][0][0][0] ``` - mime type ```bash= file --mime-type new-C0081.MP4 ``` ![](https://i.imgur.com/1kA8wwj.png)