3

I'm new to FFmpeg and trying to figure this out. I found this for Batch processing and this answer for rotation of video but I need to put them together.

Could someone please explain how to create a script for this action, on Windows?

gronostaj
  • 55,965
  • 20
  • 120
  • 179

2 Answers2

2

Basically, you just need to look the files up, (store them in a variable,) and then feed those looked up files into FFmpeg.

Of course, Windows's Batch-language would be sufficient for that. But since I have zero proficiency with that, here's a PowerShell-Script:

# Searching for files with the Get-ChildItem cmdlet and saving their relevant properties in an array:
# NOTE: -File will only work with PowerShell-versions >= 3.
[array]$FilesToRotate = Get-ChildItem -Path "C:\PATH_TO_FILES" ((-Filter *.mp4)) ((-Recurse)) -File | ForEach-Object {
    # NOTE: This part is a bit tricky - I just added it so I'm able to save the parent-path of each file in an object.
    # NOTE: One could also omit the whole ForEach-Object and use the Split-Path cmdlet inside the output-file's specification in FFmpeg's code.
    [PSCustomObject]@{
        InFullName = $_.FullName
        # Will put the output-file in the same folder as the input-file and add "_ROTATION" as suffix in its name.
        OutFullName = "$(Split-Path -Parent -Path $($_.FullName))\$($_.BaseName)_ROTATE$($_.Extension)"
    }
}

# Processing the files with FFmpeg using PowerShell's Start-Process cmdlet:
for($i=0; $i -lt $FilesToRotate.Length; $i++){
    Start-Process -FilePath "C:\PATH_TO_FFMPEG\ffmpeg.exe" -Argumentlist " -i `"$($FilesToRotate[$i].InFullName)`" -c copy -metadata:s:v:0 rotate=<x> `"$($FilesToRotate[$i].OutFullName )`" " ((-Wait)) ((-NoNewWindow))
}

This script will run FFmpeg with the code you provided (I did not check it, but you can easily replace it anyway) and save the resulting file into the same folder with the name-suffix "_ROTATE" - so "MyMovie2017.mov" will become "MyMovie2017_ROTATE.mov". (If you want to render them to a whole new folder, replace $($FilesToRotate[$i].ParentPath) with the path you like.)

Notes: things in doubled parentheses (( )) are optional:

  • -Filter will only adress (one) specific type of files, e.g. *.mp4 will only find MP4-Files. If you have more than one file-type, but many files you do not need to convert (like text-files), you could either -Exclude all formats you don't want to convert or -Include only those which should be converted (-Include is like -Filter - it is slower, but can include more than one format.)
  • -Recurse will also look into subfolders. You could also use -Depthwith PowerShell v 5+.
  • -Wait will open one ffmpeg-instance at a time - without it, all instances will be opened parallel.
  • -NoNewWindow will show the output of your ffmpeg-instance at the PowerShell-Console, while without it, every instance of ffmpeg will open in a new console-window. Makes sense only with -Wait.

You will have to delete all doubled parentheses (and the content of them if you don't want it) before starting the script.

Also, these things need to be adapted:

  • C:\PATH_TO_FILES Path to your files, obviously.
  • C:\PATH_TO_FFMPEG\ffmpeg.exe Path to your ffmpeg.exe, obviously.
  • rotate=<x> - you need to replace the <x> with either 90, 180, or 270. (As explained at the code's source)

If anything needs more explanation, I'm happy to help.

flolilo
  • 2,700
  • 1
  • 18
  • 27
  • Thanks for the help! I got it to work after some trial and error by removing your "-File" text. So it looked like this ontop: [array]$FilesToRotate = Get-ChildItem -Path "C:\PATH_TO_FILES" – ThirteenthMonth Aug 30 '17 at 00:04
  • Could you tell me what the "-File" was supposed to do? I just deleted it after it errored and got it working. – ThirteenthMonth Aug 30 '17 at 00:10
  • `-File` will prevent `Get-ChildItem` from including folders into the variable. Without `-File` (and/or `-Filter`), `$FilesToRotate` would include even subfolders, so PowerShell would try to pass your subfolder(s) as an input-file for FFmpeg. Note that we're talking about the folder itself; we're not talking about recursive search here. [The documentation of Get.-ChildItem can be found here.](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-5.1) – flolilo Aug 30 '17 at 00:18
  • @ThirteenthMonth could you tell me what error it throws? When I try it with `-File`, it works flawlessly. Also, could you tell me the version of PS you're using? (Simply type `$PSVersionTable` into the console and hit enter - it's the first value.) Thank you very much! – flolilo Aug 30 '17 at 00:33
  • Inputting $PSVersionTable I got all of this: ""PS C:\Users\> $PSVersionTable Name Value ---- ----- CLRVersion 2.0.50727.8669 BuildVersion 6.1.7601.17514 PSVersion 2.0 WSManStackVersion 2.0 PSCompatibleVersions {1.0, 2.0} SerializationVersion 1.1.0.1 PSRemotingProtocolVersion 2.1"" Yeah I removed -Recurse, -Filter for testing without, and then as soon as -File was removed it worked. It's my first time with this so idk the cause. – ThirteenthMonth Aug 30 '17 at 00:51
  • 1
    Ah - in PowerShell < version 3, the `-File`-parameter wasn't included. If you would really like to use it, I'd suggest you upgrade your PowerShell-installation, though that would be a bit overkill just to convert a few files. ;-) Anyway, I added that information into the script as a comment. – flolilo Aug 30 '17 at 01:34
0

One Line Solution

npx rotate-video --source=source_path --destination=destination_path --extension=MP4 --angel=270

Note: You need to install FFMPEG CLI first install

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 23 '22 at 21:59