Skip to content

Commit

Permalink
Allow waveform generation to happen before Clip effects are processed…
Browse files Browse the repository at this point in the history
…. Also, allow the Caption effect to add image data (i.e. waveform graphic) and allow Clip to detect that image to output it correctly. The result of all this: audio-only files now support the Caption effect, on top of the generated waveform image. Lastly, generated waveforms should use the entire Timeline size - and not a pre-determined 720x480 size.
  • Loading branch information
jonoomph committed Feb 10, 2023
1 parent 06c140c commit 2f08ac0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
53 changes: 30 additions & 23 deletions src/Clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,11 @@ std::shared_ptr<Frame> Clip::GetFrame(std::shared_ptr<openshot::Frame> backgroun
// Return the frame's number so the correct keyframes are applied.
original_frame->number = frame_number;

// Apply waveform image (if any)
if (Waveform()) {
apply_waveform(original_frame, background_frame->GetImage());
}

// Apply local effects to the frame (if any)
apply_effects(original_frame);

Expand Down Expand Up @@ -1265,36 +1270,14 @@ bool Clip::isEqual(double a, double b)
// Apply keyframes to the source frame (if any)
void Clip::apply_keyframes(std::shared_ptr<Frame> frame, std::shared_ptr<QImage> background_canvas) {
// Skip out if video was disabled or only an audio frame (no visualisation in use)
if (!Waveform() && !Reader()->info.has_video) {
if (!frame->has_image_data) {
// Skip the rest of the image processing for performance reasons
return;
}

// Get image from clip
std::shared_ptr<QImage> source_image = frame->GetImage();

/* REPLACE IMAGE WITH WAVEFORM IMAGE (IF NEEDED) */
if (Waveform())
{
// Debug output
ZmqLogger::Instance()->AppendDebugMethod(
"Clip::get_transform (Generate Waveform Image)",
"frame->number", frame->number,
"Waveform()", Waveform(),
"background_canvas->width()", background_canvas->width(),
"background_canvas->height()", background_canvas->height());

// Get the color of the waveform
int red = wave_color.red.GetInt(frame->number);
int green = wave_color.green.GetInt(frame->number);
int blue = wave_color.blue.GetInt(frame->number);
int alpha = wave_color.alpha.GetInt(frame->number);

// Generate Waveform Dynamically (the size of the timeline)
source_image = frame->GetWaveform(background_canvas->width(), background_canvas->height(), red, green, blue, alpha);
frame->AddImage(source_image);
}

// Get transform from clip's keyframes
QTransform transform = get_transform(frame, background_canvas->width(), background_canvas->height());

Expand Down Expand Up @@ -1351,6 +1334,30 @@ void Clip::apply_keyframes(std::shared_ptr<Frame> frame, std::shared_ptr<QImage>
frame->AddImage(background_canvas);
}

// Apply apply_waveform image to the source frame (if any)
void Clip::apply_waveform(std::shared_ptr<Frame> frame, std::shared_ptr<QImage> background_canvas) {
// Get image from clip
std::shared_ptr<QImage> source_image = frame->GetImage();

// Debug output
ZmqLogger::Instance()->AppendDebugMethod(
"Clip::apply_waveform (Generate Waveform Image)",
"frame->number", frame->number,
"Waveform()", Waveform(),
"background_canvas->width()", background_canvas->width(),
"background_canvas->height()", background_canvas->height());

// Get the color of the waveform
int red = wave_color.red.GetInt(frame->number);
int green = wave_color.green.GetInt(frame->number);
int blue = wave_color.blue.GetInt(frame->number);
int alpha = wave_color.alpha.GetInt(frame->number);

// Generate Waveform Dynamically (the size of the timeline)
source_image = frame->GetWaveform(background_canvas->width(), background_canvas->height(), red, green, blue, alpha);
frame->AddImage(source_image);
}

// Apply keyframes to the source frame (if any)
QTransform Clip::get_transform(std::shared_ptr<Frame> frame, int width, int height)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Clip.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ namespace openshot {
/// Apply keyframes to an openshot::Frame and use an existing QImage as a background image (if any)
void apply_keyframes(std::shared_ptr<Frame> frame, std::shared_ptr<QImage> background_canvas);

/// Apply waveform image to an openshot::Frame and use an existing QImage as a background image (if any)
void apply_waveform(std::shared_ptr<Frame> frame, std::shared_ptr<QImage> background_canvas);

/// Get QTransform from keyframes
QTransform get_transform(std::shared_ptr<Frame> frame, int width, int height);

Expand Down
10 changes: 10 additions & 0 deletions src/FFmpegReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,16 @@ void FFmpegReader::UpdateAudioInfo() {
info.video_length = info.duration * info.fps.ToDouble();
info.width = 720;
info.height = 480;

// Use timeline to set correct width & height (if any)
Clip *parent = (Clip *) ParentClip();
if (parent) {
if (parent->ParentTimeline()) {
// Set max width/height based on parent clip's timeline (if attached to a timeline)
info.width = parent->ParentTimeline()->preview_width;
info.height = parent->ParentTimeline()->preview_height;
}
}
}

// Fix invalid video lengths for certain types of files (MP3 for example)
Expand Down

0 comments on commit 2f08ac0

Please sign in to comment.