背景和問題

背景和問題

背景和問題

我用我的 MotoG3 手機 Android 手機錄製了一個視頻,生成的視頻(可能是由於晃動手機)旋轉播放,即垂直播放,這不是我想要的。影片保存在 MP4 容器中。

在檢查錄製的檔案時,我看到(同時使用ffprobemediainfo)它包含一個標誌,指示視訊串流旋轉了 90 度,這解釋了播放影片時的旋轉。這是以下的輸出ffprobe

$ ffprobe -hide_banner ~/Pictures/2016/06/19/vid_20160619_170845475.mp4 
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/rbrito/Pictures/2016/06/19/vid_20160619_170845475.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2016-06-19 20:25:49
    com.android.version: 6.0
  Duration: 00:17:01.96, start: 0.000000, bitrate: 17134 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 17000 kb/s, SAR 1:1 DAR 16:9, 29 fps, 29.42 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      rotate          : 90
      creation_time   : 2016-06-19 20:25:49
      handler_name    : VideoHandle
      encoder         : MOTO
    Side data:
      displaymatrix: rotation of -90.00 degrees
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      creation_time   : 2016-06-19 20:25:49
      handler_name    : SoundHandle

由於該影片來自我兒子的罕見活動,因此我想對影片檔案進行最小程度的更改,最好只刪除旋轉元數據,而不刪除其他內容。

不幸的是,使用來自其他答案沒有幫助。我嘗試使用:

ffmpeg -i ~/Pictures/2016/06/19/vid_20160619_170845475.mp4 -c copy -metadata:s:v:0 rotate=0 -an vid_20160619_170845475_unrotated.mp4

但其他元資料(例如日期等)不會出現在結果檔案中。尤其是,這使得組織者程式在按日期顯示文件等時感到困惑。

有沒有辦法最小化地改變輸入資料(可能重新混合它),以使其盡可能接近原始資料?特別是,我不想重新編碼該文件。

Linux 下可用的任何工具都是首選。


額外的信息

由於詢問了 f​​fmpeg 的完整輸出,所以就這樣了。這是一個純粹的 Debian 測試用戶區。

附帶說明一下,ffmpeg 抱怨庫不匹配,因為它在 Debian 中編譯了兩次,一次沒有可能有問題的編解碼器,另一次帶有一些額外的編解碼器。我使用帶有額外編解碼器的版本,但我認為這並不重要,因為這裡只使用了復用器。

重新混合先前的元數據

$ ffprobe  -hide_banner ~/Pictures/2016/06/19/vid_20160619_170845475.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/rbrito/Pictures/2016/06/19/vid_20160619_170845475.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2016-06-19 20:25:49
    com.android.version: 6.0
  Duration: 00:17:01.96, start: 0.000000, bitrate: 17134 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 17000 kb/s, SAR 1:1 DAR 16:9, 29 fps, 29.42 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      rotate          : 90
      creation_time   : 2016-06-19 20:25:49
      handler_name    : VideoHandle
      encoder         : MOTO
    Side data:
      displaymatrix: rotation of -90.00 degrees
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      creation_time   : 2016-06-19 20:25:49
      handler_name    : SoundHandle

重新復用過程

請注意,此處未複製音頻元數據(由於該-metadata:s:v:0選項,這是我所期望的),並且 ffmpeg 告訴我它正在複製視頻元數據(沒有旋轉設置)。

$ ffmpeg -hide_banner -i ~/Pictures/2016/06/19/vid_20160619_170845475.mp4 -c copy -metadata:s:v:0 rotate=0 -an vid_20160619_170845475_unrotated.mp4 
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/rbrito/Pictures/2016/06/19/vid_20160619_170845475.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2016-06-19 20:25:49
    com.android.version: 6.0
  Duration: 00:17:01.96, start: 0.000000, bitrate: 17134 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 17000 kb/s, SAR 1:1 DAR 16:9, 29 fps, 29.42 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      rotate          : 90
      creation_time   : 2016-06-19 20:25:49
      handler_name    : VideoHandle
      encoder         : MOTO
    Side data:
      displaymatrix: rotation of -90.00 degrees
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      creation_time   : 2016-06-19 20:25:49
      handler_name    : SoundHandle
Output #0, mp4, to 'vid_20160619_170845475_unrotated.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    com.android.version: 6.0
    encoder         : Lavf57.25.100
    Stream #0:0(eng): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 17000 kb/s, 29 fps, 29.42 tbr, 90k tbn, 90k tbc (default)
    Metadata:
      encoder         : MOTO
      creation_time   : 2016-06-19 20:25:49
      handler_name    : VideoHandle
      rotate          : 0
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
frame=29640 fps=403 q=-1.0 Lsize= 2121038kB time=00:17:01.91 bitrate=17003.0kbits/s speed=13.9x    
video:2120800kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.011211%

重新混合後的元數據

這裡請注意,輸出中沒有視訊元數據,只有容器級元數據。

$ ffprobe -hide_banner vid_20160619_170845475_unrotated.mp4 
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'vid_20160619_170845475_unrotated.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.25.100
  Duration: 00:17:01.95, start: 0.000000, bitrate: 17002 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 17000 kb/s, 29 fps, 29.42 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      handler_name    : VideoHandler

答案1

預設情況下,FFmpeg 不會傳輸大多數用戶元數據,但它會傳輸旋轉標籤等欄位。

下面的命令指示 FFmpeg 傳輸所有元數據,但隨後覆蓋旋轉標籤值。

ffmpeg -i in.mp4 -c copy -map_metadata 0 -metadata:s:v:0 rotate=0 -an out.mp4

答案2

運行這個命令:

ffmpeg -i oldfile.mp4 -codec copy -metadata title="" -metadata album="" -metadata year="" -metadata container="" -metadata artist="" -metadata comment="" newfile.mp4

透過在每個元資料類型中使用“”,它將清空舊的元數據,然後 ffmpeg 將整個檔案複製到清除元資料的新檔案。

建立新文件後,您需要刪除舊文件以避免重複。

答案3

  • 安裝誘變Python庫:

    python3.6 -m pip install --user mutagen
    

    或者:

    python2.7 -m pip install --user mutagen
    

    Mutagen 是一個用於處理音訊元資料的 Python 模組。

  • 將以下內容另存為,metadata_cleaner.py但您可以將其命名為任何名稱。

  • 更改VIDEO_FOLDER = 'your_full_path_for_your_videos_folder'變數。

  • 修改「您的變更」行之間要覆寫的屬性。

  • 到處跑python metadata_cleaner.py

這是腳本:

import os
from mutagen.mp4 import MP4
## Can import MP3 as well if you want to modify MP3 files

## Folder full path to the files you want to modify:

FILES_FOLDER = "/media/removable/64GB-Micro/FilesToClean"

def get_all_files(path):
    """Returns all the files in the path as a list
    """
    return os.listdir(path)

def clean_metadata(videos_dir, files):
    """
    Receives two variables videos_dir (full file folder path) and files (list)
    Iterates over all the files in the directory and creates full paths for 
    every file using the videos_dir path and os.path.join function.
    """
    for file_name in files:
        file_path = os.path.join(videos_dir, file_name)

        ## create MP4 video_file instance

        video_file = MP4(file_path)

        ## This line will print out all key-value for the video_file-s:
        ## Useful if you don't know which attributes you want to modify yet

        print(video_file)

        ## In this example I wanted to clean the ART, aArt = Artist metadata
        ## and nam = Song Name metadata
        ## These prints will show artists and titles for each file before clean
        ## -------------------------- Your changes ---------------------------
        print("file_name: {}".format(file_name))
        print("Old ART: {}".format(video_file.get('\xa9ART', "Key not found")))
        print("Old aArt: {}".format(video_file.get('aART', "Key not found")))
        print("Old nam: {}".format(video_file.get('\xa9nam', "Key not found")))

        ## clean artist and name
        ## This part will overwrite the artist and song name attributes

        video_file['\xa9ART'] = ''
        video_file['aART'] = ''
        video_file['\xa9nam'] = ''
        ## -------------------------- Your changes ---------------------------
        ## save new metadata

        video_file.save()

        ## show video_file artist and title after clean
        ## Just to see if our actions were successful print out song metadata
        ## again after the changes

        print("New ART: {}".format(video_file.get('\xa9ART', "Key not found")))
        print("New aArt: {}".format(video_file.get('aART', "Key not found")))
        print("New nam: {}".format(video_file.get('\xa9nam', "Key not found")))


if __name__ == '__main__':
    ## load all the files into files variable as a list

    files = get_all_files(FILES_FOLDER)

    ## run clean_metadata function to clean all the files in the VIDEO_FOLDER
    ## path
    clean_metadata(videos_dir=FILES_FOLDER, files=files)

相關內容