How to flip an mp4 video horizontally in python? - python

I have looked into this in moviepy and ffmpeg but could only find how to rotate a video, and not flip it horizontally.

Since the question is tagged moviepy:
from moviepy.editor import VideoFileClip, vfx
clip = VideoFileClip('video.mp4')
reversed_clip = clip.fx(vfx.mirror_x)
reversed_clip.write_videofile('new_video.mp4')
See this page for a general list of predefined effects.

In ffmpeg, its easy to flip a video horizontally:
import ffmpeg
stream = ffmpeg.input('input.mp4')
stream = ffmpeg.hflip(stream)
stream = ffmpeg.output(stream, 'output.mp4')
ffmpeg.run(stream)
Reference: ffmpeg-python Github
Well, you haven't mentioned that you need to preserve the audio as well.
But, if you want to preserve audio in your clip you can do the following. Note, I have used moviepy library.
from moviepy.editor import VideoFileClip, vfx
video = VideoFileClip('sample.mp4')
out = video.fx(vfx.mirror_x)
out.write_videofile('out.mp4')

To complement the accepted answer, the code below preserves the audio when flipping with ffmpeg-python:
input_video = ffmpeg.input('input.mp4')
flipped_video = input_video.hflip()
audio = input_video.audio
out = ffmpeg.output(flipped_video, audio, 'output.mp4')
out.run()
If the video has no audio, you'll need to catch the exception.
Ref: https://github.com/kkroening/ffmpeg-python/tree/master/examples#audiovideo-pipeline

Related

Combine image and audio together using moviepy in python

I been trying this for a long time, but doesn't seem to work, I have tried using multiple codes, this is one of them that i'm currently using but doesn't seem to work:
from moviepy.editor import *
image = ImageClip("image.jpg")
audio = AudioFileClip("audio.mp3")
video = CompositeVideoClip([image.set_duration(audio.duration)])
video = video.set_audio(audio)
video.write_videofile("output.mp4", fps=24)
Try This way it should give u the desired result:
from moviepy.editor import *
# Import the audio(Insert to location of your audio instead of audioClip.mp3)
audio = AudioFileClip("audio.mp3")
# Import the Image and set its duration same as the audio (Insert the location
of your photo instead of photo.jpg)
clip = ImageClip("image.jpg").set_duration(audio.duration)
# Set the audio of the clip
clip = clip.set_audio(audio)
# Export the clip
clip.write_videofile("video.mp4", fps=24)

How can i set an audio file for video clip with moviepy without re encoding

So far i tried:
from moviepy.editor import *
videoclip = VideoFileClip("filename.mp4")
audioclip = AudioFileClip("audioname.mp3")
new_audioclip = CompositeAudioClip([audioclip])
videoclip.audio = new_audioclip
videoclip.write_videofile("new_filename.mp4")
But it takes very long time.
I'd like to do it without re encoding. i also prefer opening video or audio clip from bytes in moviepy
One way to do it is using ffmpeg_merge_video_audio from FFMPEG tools.
ffmpeg_merge_video_audio - merges video file video and audio file audio into one movie file output.
By default the merging is performed without re-encoding.
Code sample:
from moviepy.video.io import ffmpeg_tools
ffmpeg_tools.ffmpeg_merge_video_audio("filename.mp4", "audioname.mp3", 'new_filename.mp4') # Merge audio and video without re-encoding
Note:
As far as I know, it's not possible to do it "from bytes" using MoviePy.

Add audio to video using moviepy

Title basically explains it. Trying to add audio to a video. I have a sequence of mp3 files that I want to play sequentially during the video. There're no errors or anything with the code but when I run the code theres still no audio in the video. Just a recreation of the old video with no audio (the original video doesnt have audio either).
video = VideoFileClip("finished_video.mp4")
title_clip = AudioFileClip("title.mp3")
audio_list = [title_clip]
for x in range(1, counter):
audio = AudioFileClip("p{}.mp3".format(x))
audio_list.append(audio)
video.set_audio(audio_list)
video.write_videofile("new_filename.mp4")
it works for me
from moviepy.editor import *
videoclip = VideoFileClip("sample.mp4")
audioclip = AudioFileClip("sample_audio.mp3")
new_audioclip = CompositeAudioClip([audioclip])
videoclip.audio = new_audioclip
videoclip.write_videofile("output.mp4")
use CompositeAudioClip to merge both files.
I had the same issue. It seems like set_audio() doesn't actually set it, but returns the new video combined with the audio.
This is the way I did it :
final_video = video.set_audio(audio_list)
final_video.write_videofile("new_filename.mp4")
Hope that helps !

Load video from url in moviepy and save frame

from moviepy.editor import *
clip = ( VideoFileClip("https://filelink/file.mp4"))
clip.save_frame("frame.png", t = 3)
I am able to load video using moviepy but its loading complete video and then saving the frame. Is it possible not to load the complete video but only first four second and then save the frame at 3 second.
Unless I missed something, it's not possible using MoviePy.
You may use ffmpeg-python instead.
Here is a code sample using ffmpeg-python:
import ffmpeg
stream_url = "https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4"
# Input seeking example: https://trac.ffmpeg.org/wiki/Seeking
(
ffmpeg
.input(stream_url, ss='00:00:03') # Seek to third second
.output("frame.png", pix_fmt='rgb24', frames='1') # Select PNG codec in RGB color space and one frame.
.overwrite_output()
.run()
)
Notes:
The solution may not work for all mp4 URL files, because mp4 format is not so "WEB friendly" - I think the moov atom must be located at the beginning of the file.
You may need to manually install FFmpeg command line tool (but it supposed to be installed with MoviePy).
Result frame:

MoviePy: Concatenating video clips causes weird glitches in final video

Is there a way to successfully always patch up any clips together in such a way that prevents weird glitches? I put together a .mp4 from smaller .mp4 files and I got a final video with weird glitches. I am running Python 3.6.1 on Windows 10 through Sublime Text 3. I used MoviePy to do the concatenation.
The code:
from moviepy.editor import VideoFileClip, concatenate_videoclips
import os.path
path = "C:/Users/blah/videos/out/"
cliparray = []
for filename in os.listdir(path):
cliparray.append(VideoFileClip(path + filename))
final_clip = concatenate_videoclips(cliparray)
final_clip.write_videofile(path + "concatenatedvideo.mp4", codec = "libx264")
The weird glitches:
One of the clips turns into a 3x3 grid of smaller clips.
Another has the audio not lined up with the video
Another is sped up faster than what was normal.
I had also glitch while concatenating different video clips. Some had different resolutions and that was making output video file with some sort of glitches. I fixed it with
final_clip = concatenate_videoclips(cliparray, method='compose')
Resulting output was without any glitch, but since they have different resolutions, the moviepy assigns highest resolution among video clips. To fix this you might just crop to same size.
from moviepy.editor import *
#load video 1 in to variable
video_1 = VideoFileClip('video1.mp4')
#load video 2 in to variable
video_2 = VideoFileClip('video2.mp4')
clips = [video_1, video_2]
# concatenating both the clips
final = concatenate_videoclips(clips,method='compose')
#writing the video into a file / saving the combined video
final.write_videofile("merged.mp4")

Categories